Question

I have populated a list view. which show up like

123 apple
456 Linux
789 windows

I have also added onItemClick listener to it and by displaying toast I can check the id of pressed listview. As I have a long list of approx. 1000 items, I want that if item 1 is clicked I should get the listview. some thing like

String clicked_Item = 123 apple. 

how can I do it. here is my itm listener.

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long id) {
Toast.makeText(getActivity(), "item clicked id="+id, Toast.LENGTH_LONG).show();
}
}); 
Was it helpful?

Solution

In your adapter, you should have a method called getItem(position) So, in your onItemClickListener, the int arg2 means the position of the list that was clicked.

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long id) {
        Object object = adapter.getItem(position);
        Toast.makeText(getActivity(), "item clicked id="+id, Toast.LENGTH_LONG).show();
    }
});

So, this object might be your model, wich means that this object has every information you want. So, just take what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top