I have created a custom ArrayAdapter that fills a ListView. The data in the adapter is a list of POJOs which I get from a server. Each POJO has a unique id given by the server, I have no influence on that id. The id is only for internal use, means it isn’t displayed to the user on the ListView.
My intention: if the user clicks an item in the list, I want to pass the id back to the server. The first step would be to create an onItemClickListener:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void OnItemClick(AdapterView<?> parent, View view, int position, long id) {
// call the server in a new thread
// BUT: how do I get the POJO id from this position?
}
});
My problem is: I don’t know how to obtain the id of the POJO. Since it is not displayed to the user, is is not in the TextView I get passed in the listener. The other parameters – position and id – do not seem to be what I need also. Description:
- position: The position of the view in the adapter.
- id: The row id of the item that was clicked.
I hope you got what I mean, otherwise tell me. Thank you!
That would depend on how your adapter work. The
idparameter will have a correspondence to one of the items the adapter is adapting. Based on thatid, and how your adapter works, you can retrieve the original logical item that was represented by the view that was clicked.