How To Get The Item Id In An Onitemclick Handler
I have a category table with two columns category_id and name. I have created a data helper class named CategoryDataHelper. I have a method named getCategoryCursor() of that helper
Solution 1:
You probably should get the cursor from the adapter. This way if your cursor gets replaced you are still are still getting a valid cursor.
Cursorcursor= ((SimpleCursorAdapter) adapterView).getCursor();
cursor.moveToPosition(position);
longcategoryId= cursor.getLong(cursor.getColumnIndex(CategoryDataHelper.ID));
or use "category_id"
or whatever the name of your column is in place of CategoryDataHelper.ID
.
Solution 2:
Thanks Zack, I could solve with your post...Excelent!!! ... I send a parameter from an activity to another so:
IntentmyIntent=newIntent(Clientes.this, Edc.class);
Cursorcursor= (Cursor) adapter.getItem(position);
myIntent.putExtra("CLIENTE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(myIntent);
In the other activity (EDC)....i get the parameter so:
int _clienteId = getIntent().getIntExtra("CLIENTE_ID", 0);
Solution 3:
How about in onItemclick:
categoryCursor.moveToPosition(position);
and then from the returned cursor get the ID from your helper?
Solution 4:
With the SimpleCursorAdapter
, the onItemClick
function passes in the databases id for the selected item. So, the solution is simply
long category_id = id
Post a Comment for "How To Get The Item Id In An Onitemclick Handler"