O
O
Orkhan Hasanli2018-05-11 12:16:43
Android
Orkhan Hasanli, 2018-05-11 12:16:43

How to get the id or position of an element on click?

I am using RecyclerView + SQLite to store and display items. When clicking, you need to pass the ID (of this element from the database) through the intent to another activity.
There is a method that implements getting data from the database (_id and product name).
If you specify the code below, then when you click on an element in another activity, I get numbers from 3 to 9 (this is clearly not the ID of the selected element).

descriptionIntent.putExtra("drugID", mCursor.getString(mCursor.getColumnIndex("_id")));

If you specify this code, then I get null
descriptionIntent.putExtra("drugID", mCursor.getPosition());

How do you get the id of an element?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JimmDiGreez, 2018-05-11
@azerphoenix

Write down the id at the stage of obtaining from the source. For example map to data-classes. Cursor at a particular moment always looks at one particular line, so you get not the IDs of the elements you clicked on, but the ID from the current cursor position.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question