onListItemClick is not working for listview?

前端 未结 9 2185
一整个雨季
一整个雨季 2020-11-30 11:29

Hi onListItemClick for listview is not working. Here i am fetching datas from SQLite using AsyncTask and displaying it in a list view. And i wants to do some actions when a

9条回答
  •  孤街浪徒
    2020-11-30 12:13

    The workaround I found avoid the

    AdapterView.OnItemClickListener mMessageClickedHandler=new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
    
    
            }
        };
    

    on the ListView, but exploit the Adapter constructor that takes a Context as parameter:

    myCustomAdapter=new MyCustomAdapter(ActivityName.this,...)
    

    Passing ActivityName.this is possible to cast the Context in the adapter's class as ActivityName in a safe way and use its methods working like callbacks:

    ((ActivityName)context).activityMethod()
    

    Given that the getView() method of the Adapter class has a position parameter, is possible to pass this value to the activityMethod(int position) in order to know which list item has been pressed into the Activity where the ListView is.

提交回复
热议问题