I'm brand new to Android development... coming from iPhone and .Net background. I've seen very similar questions to this one, but none of them dealt with the SimpleCursorAdapter.
I have a basic ListActivity which uses a Cursor to bind data from a SQLite query to my ListView:
ListAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
c,
new String[] {"name"},
new int[] {android.R.id.text1});
setListAdapter(adapter);
Then when an item is clicked:
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Difference between this:
Cursor c = (cursor)l.getItemAtPosition(position);
//and this??
Cursor c = (Cursor)l.getAdapter().getItem(position);
int categoryId = c.getInt(0);
}
Is this the proper way to get the id of the element that was selected? It seems strange, because I wouldn't think I could use my cursor after the database is closed (which is is after I bind). What is the point of the id passed in, when I can't seem to find a way of getting the actual item from that id? Also, I don't understand why the getItemAtPosition() would return a cursor... the cursor is bound to the entire list; not just one row. Finally, if this is correct, is there a difference between the two ways shown of getting the cursor? Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…