accessing custom objects in a listview

好久不见. 提交于 2019-12-04 06:15:30

As per my concern, assign the onClick to the ListView in the original activity is good idea, rather then assigning it to in getView() of your custom adapter. Because in getView() of your adapter it always create a new View.onClick()'s object..

list.setOnItemClickListener(new OnItemClickListener()
 {
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
  {
   adapter.get(position);  
   // Get data from your adapter,   the above code of line give the custom adapter's object of   current position of selected list item     
  }   
 });  

Agree with user370305's answer.

You just need to implement OnItemClickListener in your Activity class, after creating you will get onItemClick method, inside that you will get position of the clicked object.

 @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
        // TODO Auto-generated method stub
        MyObject clickedObject = (MyObject) adapter.getItem(position);
        // MyObject is user-defined class

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!