Identify the item clicked on ListActivity method onListItemClick
I'm developing an 开发者_运维技巧Android application.
I have several objects loaded on a ListActivity
. I want to know the item clicked on event onListItemClick
.
I see method onListItemClick
has a parameter called id. How can I setup this id?
My objects are identified by an ID, how can I add this ID to listItem?
Thanks.
if SectionObj is your object that you want to access later, set that in the adapter when you set the source.
ArrayList<SectionObj> li
ArrayAdapter<SectionObj> adapter=new ArrayAdapter<SectionObj>(getApplicationContext(),android.R.layout.simple_list_item_1, li);
setListAdapter(adapter);
then in ur listener method..
protected void onListItemClick(ListView l, View v, int position, long id) {
SectionObj o=(SectionObj)getListView().getItemAtPosition(position);
}
What is the source of your list data? If you are using a cursor - then the id passed in onListItemClick(ListView l, View v, int position, long id)
will automatically be the id of the cursor row.
Use the following;
listView.getItemAtPosition(position);
Where listView is the name of your list view.
You can set the id in your ArrayAdapter view. Checkout the following pages then you should find the solution.
http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView,%20android.view.View,%20int,%20long)
http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications
精彩评论