How to get the item id in an onItemClick handler

前端 未结 4 460
花落未央
花落未央 2020-12-16 06:24

I have a category table with two columns category_id and name. I have created a data helper class named CategoryDataHelper. I have a m

4条回答
  •  粉色の甜心
    2020-12-16 07:11

    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.

提交回复
热议问题