Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter the RecyclerView list using the button

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading