Git Product home page Git Product logo

recyclerview_search's Introduction

Recyclerview_Search

Search on Recyclerivew with EditText in Android


in the first step inside the adapter class,create a new list of models as follows:

List<ModelName> filteritem;

Notice : here, for example, we (filteritem) the name of the object in the second step,you must new an ArrayList for the object created from List as follows:

filteritem = new ArrayList<>();
filteritem = item

Notice : (item) is the intial List made of the Model

in the next step outside the viewholder we add a new class called setFilter containing the following code to the project :

 public void setFilter (String namefilter){
        namefilter =namefilter.toLowerCase();
        if (namefilter.isEmpty()){
            filteritem = new ArrayList<>();
            filteritem = item;
        }
        else {
            filteritem = new ArrayList<>();
            for (int i = 0 ; i < item.size() ; i++)
            {
                if (item.get(i).getName().toLowerCase().contains(namefilter)){
                    filteritem.add(item.get(i));
                }
            }
        }
        notifyDataSetChanged();
    }

in the next step, we will replace the new list (filteritem) with the previous list (item) in onBindViewHolder and getItemCount

Model model = filteritem.get(position)
return filteritem.size();

then go to the MainActivity and create an EditText object, then findviewbyid it:

EditText search;
search = findviewById (R.id.txtsearch);

finally, add the TextChangeListener method to it and put the following code in the on TextChanged section :

search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                adapter.setFilter(charSequence.toString().toLowerCase());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

an example of the end of this code :

alt - Github

recyclerview_search's People

Contributors

mojtabagolab avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.