Using RecyclerView I display two texts in one item, but after filtering, they are displayed asynchronously.

The reason after filtering is to create a new ArrayList , which is filled with data that matches the ones I conducted in the search. Thus, the first field is filled, and the second is filled in by position, respectively, the position after entering changes and the second field does not coincide with the first one.

enter image description here

Adaptar

 public class CheeseAdapter extends RecyclerView.Adapter<CheeseAdapter.Holder> { private final LayoutInflater mInflater; private List<String> mDefaultCheeses; private List<String> mDefaultCheeses2; private List<String> mFilteredCheeses; private List<String> mFilteredCheeses2; List<Integer> arrImage; public CheeseAdapter(LayoutInflater inflater, String[] titleTab, String[] prichTab, List<Integer> arrImage) { mInflater = inflater; // получаю массив для первого пункта mDefaultCheeses = Arrays.asList(titleTab); mFilteredCheeses = mDefaultCheeses; // для второго mDefaultCheeses2 = Arrays.asList(prichTab); mFilteredCheeses2 = mDefaultCheeses2; // картинку this.arrImage=arrImage; } @Override public Holder onCreateViewHolder(ViewGroup parent, int viewType) { return new Holder(mInflater.inflate(R.layout.item_layout, parent, false)); } @Override public void onBindViewHolder(Holder holder, int position) { View itemView = holder.itemView; TextView title = (TextView) itemView.findViewById(R.id.cheeseName); TextView title2 = (TextView) itemView.findViewById(R.id.person_age); ImageView personPhoto = (ImageView) itemView.findViewById(R.id.person_photo); // вставляем в первый TextView title.setText(mFilteredCheeses.get(position)); // второй title2.setText(mFilteredCheeses2.get(position)); // картинка personPhoto.setImageResource(arrImage.get(position)); } @Override public int getItemCount() { return mFilteredCheeses.size(); } public void filter(String query) { mFilteredCheeses = new ArrayList<>(); // находит совпадения for (String cheese : mDefaultCheeses) { if(cheese.toLowerCase().contains(query.toLowerCase())) { // добавляет в ArrayList mFilteredCheeses.add(cheese); } } // обновляется список notifyDataSetChanged(); } public class Holder extends RecyclerView.ViewHolder { public Holder(View itemView) { super(itemView); } } } 
  • I tried, but I did not understand how to separate them. - Artsait
  • For example, how to insert a search in the filter by headlines only - Artsait 4:47 pm
  • Can you please tell me how to get the headers? You can get one heading by typing persons.get (3) .title, but how do you get all the headlines? If you just keep people.title, it does not work - Artsait
  • Everything earned) Thank you - Artsait

2 answers 2

  1. No need to create a new list object. Clean the old and refill it

     //mFilteredCheeses = new ArrayList<>(); mFilteredCheeses.clear(); //наполняем новыми данными 
  2. You replace the data in only one list, and the second list and the array of images remain unchanged.

    From comments:

    For all this to work, the data transmitted to the adapter must be related (for example, a collection of POJO-models, each of which contains both an ID of a picture and a title and a value for one of the items), and not divided as you have. In its current form, the solution will be a crutch and irrational.

    To organize a filter, you get headings from models and compare, leave those that match - you get filtered related data.

    To get only models in which the headers match the condition, go through the collection of models in a loop:

     for (Person user:persons) { if ( user.title.equals("some")) {// действие при совпадении} ; } 

    where Person is a model class.

    This code will select all headers that match the word "some" in the persons collection.