Find item position in RecyclerView from data list

I want to get the position of an element in RecyclerViewfrom a dataset RecyclerView. Each item in my dataset contains a unique identifier . I want to find the position of the element’s adapter in RecyclerViewcorresponding to the identifier in the dataset.

My dataset class is as follows.

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

I'm adding TextViewor EditTextin RecyclerViewaccording to the type.

Final goal: My RecyclerViewcan have several EditTextand TextViewswhich are independent (although it TextViewwill have a field that has an identifier EditTextit is associated with). When I click TextView, I will have an identifier EditText(dataset) to receive the data. I want to get the text from the corresponding EditTextfrom the element in RecyclerViewwith these identifiers. If I can get the adapter position from the dataset, I can somehow use

recyclerview.getChildAt(position);

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

+4
source share
1 answer
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 identifier of each model.

+1
source

Source: https://habr.com/ru/post/1694437/


All Articles