How to get id from database on click of listview item in android

后端 未结 1 1720
逝去的感伤
逝去的感伤 2021-01-19 00:30

I have searched through various questions related to this on this website but i am unable to resolve the issue i am getting.

I want to get id from database on click

1条回答
  •  悲哀的现实
    2021-01-19 01:01

    You are not querying the _id from the database (only the KEY_NAME2 column), so you're not able to get it from the adapter.

    This line:

    Cursor cur = (Cursor) parent.getItemAtPosition(position);
    

    is entirely wrong. You are trying to cast a String (which is returned by ArrayAdapter to a cursor, which can never work.

    What you have to do, is use a CursorAdapter (or SimpleCursorAdapter) for your ListView. The cursor should query at least for _id and KEY_NAME2.

    With this adapter the getItem(int position) will return a cursor set to requested position. Then all you need to do is cursor.getInt(cursor.getColumnIndex("_id")) and you're there.

    0 讨论(0)
提交回复
热议问题