简体   繁体   中英

Find position of an item in RecyclerView from the data list

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM