how to get object from listview in setOnItemClickListener in android?

爷,独闯天下 提交于 2019-12-29 05:07:19

问题


I have added arraylist in arrayadapter which contains objects each consists of two elements/items, I have successfully set that adapter for setListAdapter, now i want to get those items in setOnItemClickListener of listview.

here is my code

   TweetListAdaptor adaptor = new TweetListAdaptor(this,R.layout.list_item, tweets);       
   setListAdapter(adaptor); 
   ListView lv = getListView();
   lv.setTextFilterEnabled(true);
   lv.setOnItemClickListener(new OnItemClickListener() 
   {
   public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
   {
     //here i want to get the items             
   }
 });

回答1:


public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int color = parent.getAdapter().getItem(position);
}



回答2:


public void onItemClick(AdapterView<?> parent, View view,int position, long id){
    something = tweets[position];
}



回答3:


You want to get the items and do what with them?

For example, you can make a Toast message like this.

public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
    {
        Toast.makeText(getApplicationContext(), tweets[position], Toast.LENGTH_SHORT).show();

    }

Hope this helps.




回答4:


and if you have the listview ready with all datas and want get a value of a objet only use:

ViewGroup row = (ViewGroup) listprod.getChildAt(0);
TextView tvTest = (TextView) row.findViewById(R.id.textnomprod);

Where my listview is "listprod" and i want get the value in the position 0, where textnomprod is my objet and im saving in my variable tvTest




回答5:


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
        Windows clickedObject = adapter.get(position);
    }
}

Consider in the example above,

  • object used in the listview is named Windows,
  • the object of the item clicked in the listview is named clickedObject,
  • the arraylist used is called adapter.

Also, make sure to prefix your ArrayList with final.




回答6:


I solved this problem by using its adapter that was set to it.

TweetListAdaptor adaptor = new TweetListAdaptor(this,R.layout.list_item, tweets);       
setListAdapter(adaptor); 
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
   {    
        //here i want to get the items  
        adaptor.getItem(position);   // this is your object
    }
});

Just keep in mind that the adapter must be initialized and set to the ListView.

In this way, you can access the properties of the object you want.




回答7:


getListView().getItemAtPosition(position) will return an Object in your TweetListAdaptor



来源:https://stackoverflow.com/questions/7073577/how-to-get-object-from-listview-in-setonitemclicklistener-in-android

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