How to get the item id in an onItemClick handler

為{幸葍}努か 提交于 2019-11-29 02:34:04

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.

Cursor cursor = ((SimpleCursorAdapter) adapterView).getCursor();
cursor.moveToPosition(position);
long categoryId = cursor.getLong(cursor.getColumnIndex(CategoryDataHelper.ID));

or use "category_id" or whatever the name of your column is in place of CategoryDataHelper.ID.

ag123

Thanks Zack, I could solve with your post...Excelent!!! ... I send a parameter from an activity to another so:

Intent myIntent = new Intent(Clientes.this, Edc.class);
Cursor cursor = (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);

How about in onItemclick:

categoryCursor.moveToPosition(position);

and then from the returned cursor get the ID from your helper?

With the SimpleCursorAdapter, the onItemClick function passes in the databases id for the selected item. So, the solution is simply

long category_id = id
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!