17

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             
   }
 });
Akram
  • 7,548
  • 8
  • 45
  • 72
Ramamoorthy
  • 287
  • 3
  • 6
  • 10

7 Answers7

57
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int color = parent.getAdapter().getItem(position);
}
u-rick
  • 571
  • 4
  • 2
7
public void onItemClick(AdapterView<?> parent, View view,int position, long id){
    something = tweets[position];
}
Cristian
  • 198,401
  • 62
  • 356
  • 264
2

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

2

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.

utamanna
  • 189
  • 2
  • 10
0
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.

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
Mishka
  • 1
  • 1
0

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Mahdi Moqadasi
  • 2,029
  • 4
  • 26
  • 52
-2

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

AlanS
  • 738
  • 1
  • 6
  • 13