U
U
un1t2020-10-04 21:12:18
Android
un1t, 2020-10-04 21:12:18

Why doesn't setOnItemClickListener work on ListView?

Understanding ListView. The list is displayed, but when clicked, nothing happens.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var listView = findViewById<ListView>(R.id.lv_list)
        listView.adapter =  MyAdapter(this)
        listView.setOnItemClickListener {parent, view, position, id ->
            Toast.makeText([email protected], "zzz", Toast.LENGTH_SHORT).show()
            Log.e("AAA", "zzz")
        }
    }
}


class MyAdapter(private var context: Context): BaseAdapter() {
    private val inflater: LayoutInflater =
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

    override fun getCount(): Int {
        return 10
    }

    override fun getItem(position: Int): Any {
        return "Item ${position + 1}"
    }

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

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val rowView = inflater.inflate(R.layout.list_item, parent, false)

        var tv_text = rowView.findViewById<TextView>(R.id.tv_text)
        tv_text.text = "Item ${position + 1}"

        return rowView
    }
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>


list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tv_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"
        android:text="1111">
    </TextView>

</LinearLayout>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-10-06
@un1t

No need to use ListView. Use RecyclerView. Place the click handler directly on the view.
In your case, I think one of the views is intercepting clicks (checkbox, I guess).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question