Jaydeep Dhamecha Jaydeep Dhamecha - 2 months ago 13
Android Question

How to compare originalList to filterList in recyclerview using Filter?

In this code I have search multiple text ex. I have search sachin.

sachin is show the list but click to back sachin to sa not display to sa list on adapter. How to solve it without any library?

Example

Suppose I have search sachin to text change sachin to sa. In this case not display sa list. I have clear all list that case display all list.

My Adapter

private class ItemFilter extends Filter {
protected FilterResults performFiltering(CharSequence constraint) {

FilterResults results = new FilterResults();

if (constraint != null && constraint.length() > 0) {
ArrayList<Build> buildlist = new ArrayList<Build>();


for (int i = 0; i < buildList.size(); i++) {
if ((buildList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {

Build babydata = new Build(buildList.get(i).getImages(), buildList.get(i).getName());

buildlist.add(babydata);
}
}
results.count = buildlist.size();
results.values = buildlist;

} else {
results.count = buildList.size();
results.values = buildList;
}
return results;

}

@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
buildList = (ArrayList<Build>) results.values;
notifyDataSetChanged();
}
}


Activity class

EditText editTxt = (EditText) findViewById(R.id.search);
editTxt.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

if (s.length() <= 0) {
Log.d("jay", "s.length() <= 0" + s.toString());
buildList.clear();
buildList.addAll(buildListCopy);
recyclerView.setAdapter(null);
buildCustomAdapter = new BuildCustomAdapter(buildList);
recyclerView.setAdapter(buildCustomAdapter);

} else {
buildCustomAdapter.getFilter().filter(s.toString());
}
Log.d("jay", "mobisharnam" + s.toString());
}

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

}

@Override
public void afterTextChanged(Editable s) {
}
});

Answer

I prefer Predicate for search filter I have changed your code to use Predicate, You have to use google guava library for that include following depedency in your gradle

compile 'com.google.guava:guava:19.0'

now, check following code

EditText editTxt = (EditText) findViewById(R.id.search);
    editTxt.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            String searchQuery = s.toString();
                if (buildListCopy != null && buildListCopy.size() > 0) {
                    buildList = Lists.newArrayList(Collections2.filter(buildListCopy, new SearchFilter(searchQuery)));
                    recyclerView.setAdapter(null);
                    if (buildList != null && buildList.size() > 0) {);
                        buildCustomAdapter = new BuildCustomAdapter(buildList);
                        recyclerView.setAdapter(buildCustomAdapter);
                        recyclerView.getAdapter().notifyDataSetChanged();
                    }
                }
                return false;
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

Here is your search filter predicate which we have used above

/**
     * Custom Predicate class to filter list as per search query
     */
    public final class SearchFilter implements Predicate<Build> {
        private final Pattern pattern;

        public SearchFilter(final String regex) {
            pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.LITERAL);
        }

        @Override
        public boolean apply(final Build input) {
            return pattern.matcher(input.getName().toLowerCase()).find();
        }
    }
Comments