Find position of an item in RecyclerView from the data list

hushed_voice :

I want to get the position of an item in RecyclerView from the dataset of the RecyclerView. Each of the item in my dataset contains a unique id. I want to find the adapter position of the item in the RecyclerView corresponding to an id in the dataset.

My dataset class looks like the following.

class dataset {
    int id;
    JSONArray linked;
    int type;
    ....
}

I add TextView or EditText to the RecyclerView according to the type.

Final aim: My RecyclerView can have multiple EditText and TextViews which are independent (although the TextView will have a field which has the id of the EditText it is linked to). When I click TextView, I will be having the id of EditText(dataset) to take data from. I want to get the text from corresponding EditText from the item in the RecyclerView with those id. If I can get the adapter position from the dataset, somehow I can use

recyclerview.getChildAt(position);

But there is no way to get the adapter position from data set.

Santanu Sur :
Hashmap<Integer,Integer> map = new Hashmap<>();
ArrayList<dataset> main_list ; // it contains all objects of dataset

for(int i = 0; i < main_list.size(); i++) {
    int id = main_list.get(i).getId(); // id of the model
    map.put(id, i); // i is the position of adapter
}

The map contains the adapter position with the id of each model..

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=471812&siteId=1