Search code examples
androidlistviewkotlin

setOnclicklistener in a listview with a customadapter


i just started using Android studio with Kotlin. I implemented a listView with a custom adapter, but i don't understand how detect when i click on a item of this listview.

This is my class CalAdapter.kt

class CalAdapter(context: Context,al_session:ArrayList<activity_session>) : BaseAdapter(){

private val mInflator: LayoutInflater
private val al_session:ArrayList<activity_session>

init {
    this.mInflator = LayoutInflater.from(context)
    this.al_session=al_session
}

override fun getCount(): Int {
    return al_session.size
}

override fun getItem(position: Int): Any {
    return al_session.get(position)
}

override fun getItemId(position: Int): Long {
    return position.toLong()
}

@SuppressLint("ResourceAsColor", "PrivateResource")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
    val view: View?
    val vh: ListRowHolder
    if (convertView == null) {
        view = this.mInflator.inflate(R.layout.calendar_adapter, parent, false)
        vh = ListRowHolder(view)
        view.tag = vh
    } else {
        view = convertView
        vh = view.tag as ListRowHolder
    }

    vh.label.text = al_session[position].id.toString()
    vh.ivImage.setBackgroundColor(R.color.material_blue_grey_800)
    when (al_session[position].provided_sport)
    {
        "swimming" ->
        {
            //vh.ivImage.setBackgroundColor(R.color.material_blue_grey_800)
            vh.ivImage.setImageResource(R.drawable.ic_swiming)

        }
        "running" ->
        {
            //vh.ivImage.setBackgroundColor(R.color.white)
            vh.ivImage.setImageResource(R.drawable.ic_running)
        }
        "cycling" ->
        {
            //vh.ivImage.setBackgroundColor(R.color.material_blue_grey_800)
            vh.ivImage.setImageResource(R.drawable.ic_bicycle)
        }
    }
    return view
}



}

private class ListRowHolder(row: View?) {
val label: TextView = row?.findViewById(R.id.TWActivityID) as TextView
val ivImage: ImageButton = row?.findViewById(R.id.CalActivityButton) as ImageButton

}

This is my function where i implement my listview.

lv is my listview.

fun createActivities()
{
    val lv = dynamicList
    val test=ArrayList<activity_session>()
    var i : Int = 0
    while (i < 5) {
        if (ActivityList.activityList[i] != null) {
            test.add(ActivityList.activityList[i])
        }
        i += 1
    }
    val obj_adapter: CalAdapter
    obj_adapter = CalAdapter(this.view!!.dynamicList.context, test)
    lv.adapter=obj_adapter
}

I created a xml file which represent my listview.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/AdapterLayout"
        android:layout_width="215dp"
        android:layout_height="140dp"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/CalActivityButton"
            android:layout_width="wrap_content"
            android:layout_height="140dp"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_info_black_24dp" />

        <TextView
            android:id="@+id/TWActivityID"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="TextView"
            android:visibility="invisible"
            tools:layout_editor_absoluteX="92dp"
            tools:layout_editor_absoluteY="230dp" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Could someone explain me how to detect when i click on an item of the listview i just created ?

EDIT : Hi again i understood how to do it, i added a setOnClickListener method to my imagebutton in my CalAdapter.kt class

now it looks like that : https://i.sstatic.net/qLNba.jpg


Solution

  • On your activity you should add a listener to the listview.

     list.setOnItemClickListener { parent, view, position, id ->
         val myItem = parent.getItemAtPosition(position) as MyDataObj
     }
    

    You should also have the getItem() method filled correctly on your adapter as you do.