Android - Getting the id of a ListView item [Special Case]
In my DB class have a method that returns a Cursor. the cursor will contain data from different tables:
public Cursor fetchFavTitles() {
return myDataBase.rawQuery("SELECT rowid as _id, title, fav FROM table1 " +
"WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table2 WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table3 WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table4 WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table5 WHERE fav = 1 UNION ALL " +
"SELECT rowid as _id, title, fav FROM table6 WHERE fav = 1", null);
}
Then, I use a SimpleCursorAdapter ca
to attach the Cursor
to a ListView lv
:
Cursor cursor = myDbHelper.fetchTitles(c);
String[] columns = {cursor.getColumnName(1)};
int[] columnsLayouts = {R.id.item_title};
ca = new SimpleCursorAdapter(this.getBaseContext(), R.layout.items_layout, cursor,columns , columnsLayouts);
lv = getListView();
lv.setAdapter(ca);
Now, if I select any item in the ListView, I want to have a reference to that item id. I want the id to retrieve other information from the table the item belong to. And retrieve it in other activity MsgView
I tried this:
have a reference to the id passed when an item clicked
pass that value with the intent, so that the other a开发者_如何学编程ctivity will retrieve the needed data using this passed value.
lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(view.getContext(), MsgView.class); i.putExtra("id", (int) id); // startActivity(i); } });
However, this didn't work. It seems that the id I'm using is not the item's id ! Any solution or ideas ?
In onItemClick method try this...
final SimpleCursorAdapter simpleCursorAdapter = (SimpleCursorAdapter) parent.getAdapter();
final Cursor cursor = simpleCursorAdapter .getCursor();
final int idColIndex = cursor.getColumnIndex(KEY_ROWID); //_id column of your table
final int rowId = cursor.getInt(idColIndex);
Used in one of my project here is link refer line number 41. Hope this help.
Edit:
Solved. See comments.
You want the position parameter not the id
See the documentation of onItemClick:
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Since: API Level 1 Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
parent: The AdapterView where the click happened.
view: The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position: The position of the view in the adapter.
id: The row id of the item that was clicked.
You could create a static data member in your MsgView
class that you use to store the id, or you can send a delayed message to the class, making sure that the activity gets loaded and started before the message arrives.
You can use the View.setTag() and View.getTag() methods to store any data in your views. Sometimes I have exploited this to 'tag' a Intent object to each row in a ListView. Whenever I need to lookup data for a row i just get the Intent back with getTag()
I found a solution to my problem.
Although it is not a professional solution, but it is the only one I have now. I added a new column in each table named: tableNumber
so that each table will have a different number. Then, I can distinguish between items based on that number.
Thanks for your answers. and special thanks to @gopal
精彩评论