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
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.