Search code examples
androidlistviewandroid-listviewandroid-eventsubitem

onclicklistener for subitems of listview


I have a listview and each item of the lisview is a linearlayout. Each one of the linearlayouts contains 3 textviews.

How do i set a onclicklistener for those textviews?

i tried this:

TextView tv=(TextView)findById(R.id.textView1);
tv.setOnClickListener(...);

This throws me a nullpointerexception.

I also tried setonitemclickedlistener for the listview,but this only allows me to operate on the linearlayout,not the textview.

thanks in advance.


Solution

  • If this is needed statically and your view is XML based, this is what I did:

    <TextView
        ...
        android:clickable="true"
        android:onClick="myHandler"
    />
    

    This calls myHandler whenever the textview is touched/clicked. As you are using this in a list view, you will still need to add a tag in getView() and use that in myHandler() to figure out which row/field were pressed.

    Hope this helps.