1

I use custom adapter for List View on my android app . I do not know how can set the listener for the item click. I use Kotlin for my app.

This is my adapter layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <CheckBox
        android:id="@+id/cbx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    <TextView
        android:id="@+id/txtNameNote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_toRightOf="@+id/cbx"
        />
    <TextView
        android:id="@+id/txtPercent"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="TextView"
        android:layout_toRightOf="@+id/cbx"
        android:layout_below="@+id/txtNameNote"
        />
    </RelativeLayout>
</LinearLayout>
This is my adapter code

class CustomAdapter(internal var context: Context, resource: Int, internal var listNote: ArrayList<Note>) : ArrayAdapter<Note>(context, resource, listNote) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        // TODO Auto-generated method stub
        val inflater = (context as Activity).layoutInflater
        convertView = inflater.inflate(R.layout.rowlistitem, parent, false)
        val name = convertView!!.findViewById<View>(R.id.txtNameNote) as TextView
        val percent = convertView.findViewById<View>(R.id.txtPercent) as TextView
        val cb = convertView.findViewById<View>(R.id.cbx) as CheckBox
        name.text = listNote[position].name
        percent.text = "Percent: " + listNote[position].percent + "%"

        if (listNote[position].status == NoteStatus.Active.value)
            cb.isChecked = false
         else{
            name.paintFlags = (name.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG)
            cb.isChecked = true
        }
       }
      }
        return convertView
    }
}

Some one can help me out ?

1 Answers1

2

The proper way in Kotlin is by creating a callback. Then you create a click listener in your Adapter and forward the result to your callback.

class CustomAdapter(
      internal var context: Context, 
      resource: Int, 
      internal var listNote: ArrayList<Note>,
      val callback: (Note) -> Unit) : ArrayAdapter<Note>(context, resource, listNote) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        // .. do your bindings whatever 
        convertView.yourRootLayoutElement.setOnClickListener{ 
           if (callback != null) callback(listNote[position])
        }
        return convertView
    }
}

In your View where you create your adapter

CustomAdapter(...., { info { "Clicked $it" } })

I've not tested it since I don't use ListView. I recommend you to use RecyclerView. ListView is deprecated and not recommended for several reasons. Read some of the reasons here.

Edit: There is absolutely no reason for using a ListView over a RecyclerView. Even if it works, you should not use it. Learn the RecyclerView since ListView should no longer used.

Emanuel
  • 8,027
  • 2
  • 37
  • 56