Error in recyclerview filter (Android Studio)?

1

I happen to have a project in android studio which performs a search in a recyclerview from a searchview, when debugging my application I see that if you filter the data well but show the data filtered in the recyclerview shows data that do not have to see with the search, here my code

MainActivity

public class InicioFragment extends Fragment {

    public RecyclerView recyclerView;
    public RecyclerView.Adapter adapter;
    public RecyclerView.LayoutManager layoutManager;
    public SearchView searchView;

    public Context context;
    public ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
    public AppConfiguration appConfiguration;

    public List<Inicio> listCreditos;

    public InicioFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_inicio, container, false);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.rclvInicio);
        recyclerView.setHasFixedSize(true);

        searchView = (SearchView) rootView.findViewById(R.id.searchInicio);

        this.context = this.getContext();
        appConfiguration = (AppConfiguration) getActivity().getApplicationContext();

        arrayList = new ArrayList<InicioItem>();
      
        arrayList = GetArrayListInicioItem();
      
      adapter = new InicioRecyclerAdapter(context, arrayList);
      
      recyclerView.setAdapter(adapter);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
             ((InicioRecyclerAdapter) adapter).getFilter().filter(newText.toString());
                return true;
            }
        });

        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        return rootView;
    }

}

Adapter

public class InicioRecyclerAdapter extends RecyclerView.Adapter<InicioRecyclerAdapter.InicioRecyclerViewHolder> implements Filterable {
    ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
    private static ArrayList<InicioItem> arrayListInicio = new ArrayList<InicioItem>();

    private ArrayList<InicioItem> inicioItemFilter;
    private ArrayList<InicioItem> filteredContactList;
    private CustomFilter mFilter;

    private LayoutInflater inflater;
    private Context context;
   
    public InicioRecyclerAdapter(Context context, ArrayList<InicioItem> arrayList) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.arrayList = arrayList;
        this.arrayListInicio = arrayList;

        this.inicioItemFilter=arrayList;
        this.filteredContactList=new ArrayList<>();
        this.filteredContactList.addAll(arrayList);
        this.mFilter = new CustomFilter(InicioRecyclerAdapter.this);
       
    }
    @Override
    public Filter getFilter() {
        return mFilter;
    }

    @Override
    public int getItemCount() {
        return filteredContactList.size();
    }

    @Override
    public InicioRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_inicio_layout, parent, false);//LayoutInflater.from(parent.getContext()) --> inflate
        InicioRecyclerViewHolder inicioRecyclerViewHolder = new InicioRecyclerViewHolder(view);
        return inicioRecyclerViewHolder;
    }        

    
    //private class Filtro extends Filter{
    public class CustomFilter extends Filter {

        private InicioRecyclerAdapter inicioRecyclerAdapter;

        private CustomFilter(InicioRecyclerAdapter inicioRecyclerAdapter) {
            super();
            this.inicioRecyclerAdapter=inicioRecyclerAdapter;
        }

        @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                filteredContactList.clear();
                final FilterResults results = new FilterResults();
            final String text = charSequence.toString().toLowerCase().trim();

                if (text != "" || text.length() != 0) {

                    for (final InicioItem item : inicioItemFilter) {
                        if (item.nombrePersona().toLowerCase().contains(text))
                           {
                            filteredContactList.add(item);
                    }

                    results.values = filteredContactList;
                    results.count = filteredContactList.size();
                }
                else
                {
                    filteredContactList.addAll(inicioItemFilter);
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            this.inicioRecyclerAdapter.notifyDataSetChanged();
            }
        }
    }
    
asked by Geek 15.08.2018 в 06:52
source

1 answer

1

What happens is that you are changing the reference from one list to another , not affecting its elements. The RecyclerView already has a reference assigned, therefore you must modify the one that is already , not change it.

Instead of doing in the publishResults method:

inicioItemFilter = (ArrayList<InicioItem>) filterResults.values;

You should do:

inicioItemFilter.clear(); // Limpiarla
inicioItemFilter.addAll((ArrayList<InicioItem>) filterResults.values); // Agregar todos los elementos del resultado

If it helps, check out this answer I did a while ago .

    
answered by 15.08.2018 / 22:48
source