Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get object from listview in setOnItemClickListener in android?

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             
   }
 });
like image 840
Ramamoorthy Avatar asked Aug 16 '11 04:08

Ramamoorthy


3 Answers

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int color = parent.getAdapter().getItem(position);
}
like image 141
u-rick Avatar answered Sep 21 '22 06:09

u-rick


public void onItemClick(AdapterView<?> parent, View view,int position, long id){
    something = tweets[position];
}
like image 22
Cristian Avatar answered Sep 20 '22 06:09

Cristian


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.

like image 26
utamanna Avatar answered Sep 20 '22 06:09

utamanna