0

I am implementing Edit text with RecyclerView where RecylecerView should update its item and its View when user enter any text in the edit text. It is a very common requirement and i have found tons of solution for this. But i m facing a strange issue that after filtering recylcerView is showing wrong item in the list.
To Illustrate. lets suppose RecylcerView contains items as a,b,c,d and e. if i search 'c. list shows only one item 'a'. however when i click on this item it is 'c' only. it means only layout not getting updated however values do get updated.
Here is the my implementation.

public class CustomFilter extends Filter{
    private CustomFilter(SListRecyclerViewAdapter mAdapter) {
        super();
    }
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        sData.clear();
        final FilterResults results = new FilterResults();
        if (constraint.length() == 0) {
            sData.addAll(filteredsData);
        }
        else {
            final String filterPattern = constraint.toString().toLowerCase().trim();
            for (final S surg: filteredsData) {
                if (S.getSUserName().toLowerCase(Locale.getDefault()).contains(filterPattern)) {
                    sData.add(surg);
                }
            }
        }
        results.values = sData;
        results.count = sData.size();
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        notifyDataSetChanged();
    }
Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
manishKumarSingh
  • 103
  • 2
  • 10
  • May be this will help you . [http://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview](http://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview) – Ranjith KP Jan 16 '17 at 11:21
  • @RanjithKP thanks, but i have gone through this.. No avail..My question is why layout is not redraw even if a called notifyDataSetChanged. – manishKumarSingh Jan 16 '17 at 11:40

2 Answers2

0

Please see the below example,you have to update the data with result.Please try

@SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mFilteredData = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }
Ranjith KP
  • 858
  • 6
  • 18
0

I dont why it is working this way. But i have found a work around. recyclerView.swapAdapter(sListRecyclerViewAdapter,false); It is basically swapping the adapter with new one but recycling earlier views. Its not very much optimal but at least workable.

manishKumarSingh
  • 103
  • 2
  • 10