Home > database >  Filter the RecyclerView list using the button
Filter the RecyclerView list using the button

Time:01-29

I found the RecyclerView filtering code via SearchView. How can I change this code for a button? When the button is pressed, the corresponding RecyclerView must be filtered. All codes:

public void updateList(List<DataHolder> list){
     displayedList = list;
     notifyDataSetChanged();
}
searchField.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // filter your list from your input
            filter(s.toString());
            //you can use runnable postDelayed like 500 ms to delay search text
        }
    });
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);
}

Thank you!

EDIT: enter image description here

CodePudding user response:

Add a button to the layout and setOnClickListener in java/kotlin file and on click of button filter the list. Like what you were doing earlier in the afterTextChanged() ( calling filter(s.toString()); )that needs to be done in setOnClickListener()

  •  Tags:  
  • Related