SimpleCursorAdapter and ViewBinder - Binding data to ListView items to be retrieved on click

后端 未结 2 1105
北荒
北荒 2021-01-21 07:22

So I\'ve got a ListView (using a ListActivity) that I\'m populating from a SQLiteDatabase. I\'m trying to attach the ID (PK) of the row t

2条回答
  •  一生所求
    2021-01-21 07:48

    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.

    @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
           Cursor cursor =  adapter.getCursor();
           cursor.moveToPosition(position);
           String id = cursor.getString(cursor.getColumnIndex("primary key field name in database");
           Toast.makeText(getBaseContext(), "ID=" + id, 1).show();
        } 
    

    NOTE : your adapter must be declared as a SimpleCursorAdapter other wise you should downcast it.

提交回复
热议问题