마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

코틀린 안드로이드 커스텀뷰 사용 관련 질문

0 추천

기존에 자바 코드를 코틀린으로 변환 중에 있습니다.

커스텀 리스트뷰에 체크박스를 추가하여 사용하는 형태입니다.

체크박스 클래스를 아래처럼 생성하여 사용했습니다.

class CheckableLinearLayout :LinearLayout, Checkable{
// 만약 CheckBox가 아닌 View를 추가한다면 아래의 변수 사용 가능.
// private boolean mIsChecked ;

    constructor(context: Context?,attrs: AttributeSet?)
    :super(context, attrs)
    {
    // mIsChecked = false ;
    }

    override fun isChecked(): Boolean {
        val cb = findViewById<View>(R.id.checkBox1) as CheckBox
        return cb.isChecked
        // return mIsChecked ;
    }

    override fun toggle() {
        val cb = findViewById<View>(R.id.checkBox1) as CheckBox
        isChecked = if (cb.isChecked) false else true
        // setChecked(mIsChecked ? false : true) ;
    }

    override fun setChecked(checked: Boolean) {
        val cb = findViewById<View>(R.id.checkBox1) as CheckBox
        if (cb.isChecked != checked) {
            cb.isChecked = checked
        }

        // CheckBox 가 아닌 View의 상태 변경.
    }
}

그리고 listview의 xml에 시작과 끝부분에 아래처럼 추가를 했습니다.

com.dkms.tablet_as.view.CheckableLinearLayout 

그런데 CheckableLinearLayout 클래스가 사용중이지 않다고 뜹니다. 회색으로요.

에러는 android.view.InflateException: Binary XML file line #2: Error inflating class com.dkms.tablet_as_Kotlin.view.CheckableLinearLayout 이런식으로 표시됩니다.

분명히 참조를 하고있는데 xml에서 control +b 를 눌러도 해당 클래스로 이동하지 않습니다.

 

프로그램 run을 하면 강제종료되면서 죽어버리는데

커스텀 리스트뷰의 클래스의 getView 부분에서 에러가 있다고 표시됩니다.

class ListViewAdapter : BaseAdapter() {
    // Adapter에 추가된 데이터를 저장하기 위한 ArrayList
    private var listViewItemList = ArrayList<ListViewItem>()

    // Adapter에 사용되는 데이터의 개수를 리턴. : 필수 구현
    override fun getCount(): Int {
        return listViewItemList.size
    }

    // position에 위치한 데이터를 화면에 출력하는데 사용될 View를 리턴. : 필수 구현
    override fun getView(position: Int,convertView: View?,parent: ViewGroup): View? {
        var convertView = convertView
        var pos = position
        var context = parent.context

        // "listview_item" Layout을 inflate하여 convertView 참조 획득.
        if (convertView == null) {
            var inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = inflater.inflate(R.layout.listview_item, parent, false)
        }

        // 화면에 표시될 View(Layout이 inflate된)으로부터 위젯에 대한 참조 획득
        var nameTextView =
            convertView!!.findViewById<View>(R.id.textView1) as TextView
        var sizeTextView =
            convertView!!.findViewById<View>(R.id.textView2) as TextView
        var dateTextView =
            convertView!!.findViewById<View>(R.id.textView3) as TextView

        // Data Set(listViewItemList)에서 position에 위치한 데이터 참조 획득
        var listViewItem = listViewItemList[position]

        // 아이템 내 각 위젯에 데이터 반영
        nameTextView.text = listViewItem.fileName
        sizeTextView.text = listViewItem.fileSize
        dateTextView.text = listViewItem.fileDate
        return convertView
    }

    // 지정한 위치(position)에 있는 데이터와 관계된 아이템(row)의 ID를 리턴. : 필수 구현
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    // 지정한 위치(position)에 있는 데이터 리턴 : 필수 구현
    override fun getItem(position: Int): Any {
        return listViewItemList[position]
    }

    fun clearItem() {
        listViewItemList.clear()
    }

    // 아이템 데이터 추가를 위한 함수
    fun addItem(name: String?, size: String?, date: String?) {
        val item = ListViewItem()
        if (name != null) {
            item.fileName = name
        }
        if (size != null) {
            item.fileSize = size
        }
        if (date != null) {
            item.fileDate = date
        }
        listViewItemList.add(item)
    }
}

혹시 어떤 원인인지 알수있을까요?

lissom (240 포인트) 님이 2020년 5월 20일 질문

1개의 답변

0 추천

View 클래스를 보시면 생성자가 여러개인 것을 보실 수 있을 겁니다. 코틀린에서는 이 부분을 서포트하기 위해 생성자를 다 만들지 않고 @JvmOverload라는 어노테이션을 사용해서 해결할 수 있습니다.

class CheckableLinearLayout @JvmOverload constructor(context: Context?, attrs: AttributeSet?= null, defStyleAttr: Int = 0) : LinearLayout, Checkable{context, attrs, defStyleAttr) {

}

익명사용자 님이 2020년 5월 26일 답변
...