-1

I have a recycler view with flight information, and I am searching using the flight number on the search bar. When I search, the recyclerview is updated dynamically for which the logic is there in my filter function, however when I clear the search, the recycler view is completely empty. How can I fix this issue?

search filter function

private void filter(String text) {
     ArrayList<FlightItem> filteredList = new ArrayList<>();

     for(FlightItem item : flightItems) {
         if(item.getFlightNumber().toLowerCase().contains(text.toLowerCase())){
             filteredList.add(item);
          }
     }
     mAdapter.filterList(filteredList);
     flightItems.clear();
     flightItems.addAll(filteredList);
   }
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
Ian Bell
  • 533
  • 5
  • 18

2 Answers2

1

keep a copy of flightItems, as you are clearing it before adding filtered items

ArrayList<FlightItem> flightItems = new ArrayList<>();
ArrayList<FlightItem> flightItemsCopy = new ArrayList<>();

private void filter(String text) {
     if(text.trim() == ""){
        clearSearch()
        return
     }
     ArrayList<FlightItem> filteredList = new ArrayList<>();

     for(FlightItem item : flightItems) {
         if(item.getFlightNumber().toLowerCase().contains(text.toLowerCase())){
             filteredList.add(item);
          }
     }
     flightItemsCopy.clear(); //clear copy
     flightItemsCopy.addAll(flightItems); // make a copy here
     mAdapter.filterList(filteredList);
     flightItems.clear();
     flightItems.addAll(filteredList);
   }
}

private void clearSearch(){
    flightItems.clear();
    flightItems.addAll(flightItemsCopy);
    mAdapter.notifyDataSetChanged();
}

Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • When text is empty in filter method, i have updated the code – Rajan Kali Feb 17 '21 at 13:32
  • where can I keep the copy of the flightItems? – Ian Bell Feb 17 '21 at 13:35
  • where you have your flightItems, mostly a global variable – Rajan Kali Feb 17 '21 at 13:38
  • In that case, wherever I am using flightItems in my activity, I need to replicate it for flightItemsCopy as well, so wont that be really inefficient? – Ian Bell Feb 17 '21 at 13:43
  • I would really appreciate if you could help me here – Ian Bell Feb 17 '21 at 13:59
  • Ideally, a Adapter has single data source which is the primary `List`, so if you are replacing it with filteredItems, then you will lose it's data, so you have to hold it anyways, what you can do is instead of adding it everywhere , you can just copy items to flightItemscopy from flightItems just before clearing in filter method, I will update answer – Rajan Kali Feb 17 '21 at 14:10
  • I have updated the filter method, no need to access flightItemsCopy anywhere else – Rajan Kali Feb 17 '21 at 14:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228852/discussion-between-ian-bell-and-rajan-kali). – Ian Bell Feb 17 '21 at 14:16
0
void filter(String text){
 List<DataHolder> temp = new ArrayList();
 for(DataHolder d: displayedList){
       //or use .equal(text) with you want equal match
       //use .toLowerCase() for better matches
       if(d.getEnglish().contains(text)){
           temp.add(d);
       }
 }
 //update recyclerview
 disp_adapter.updateList(temp);

}

updateList method :

public void updateList(List<DataHolder> list){
 displayedList = list;
 notifyDataSetChanged();

}

Bhunnu Baba
  • 1,742
  • 15
  • 22