Ayush Pahwa
Ayush Pahwa

Reputation: 163

Android recyclerview filter showing no items until searchview icon is clicked

I have a fragment in which I do a network call and populate the contained recycler view with cards. Now I followed this to add a filter to my recycler view. Now the problem arises when in the adapter constructor I do

this.storeLists = new ArrayList<>(storeLists);

the List in my fragment gets populated but doesn't show any item until I click the searchview icon and start typing something. After that even if I close the searchview the list retains.

I tried changing the above line to

this.storeLists = storeLists;

when I remove the search query the deleted items are not re shown. So in case if I search something non - existent in my recycler view list, I am left with nothing showing in my recycler view.

What I want is that when I open the fragment I see the result and then when I click the search icon and start typing the filters work as shown in the link above.

EDIT:

Here is my adapter and here is my calling fragment.

Upvotes: 0

Views: 1289

Answers (2)

Ayush Pahwa
Ayush Pahwa

Reputation: 163

After some hit and trial I found the solution at the cost of the cool animation. For now the code segment works by just resetting the adapter like this:

@Override
public boolean onQueryTextChange(String newText) {
    Log.d(TAG,newText);
    searchStoreList = storeList;
    final List<StoreList> filteredModelList = filter(searchStoreList, newText);
    //((RVStoreAdapter) mAdapter).animateTo(filteredModelList);
    //The above commented line is the old code. Following is how to reset the adapter!
    mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new RVStoreAdapter(getActivity(),storeList,session.getLat(),session.getLongi());
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.scrollToPosition(0);
    return true;
}

The animation is now very abrupt but the solution resets the list on changing the search query accordingly.

Upvotes: 1

Ragesh Ramesh
Ragesh Ramesh

Reputation: 3520

You are using this.storeLists = new ArrayList<>(storeLists); and hence notifyDatasetChanged wont work as you are using a new list altogether.

But you shouldnt change it as you need the keep the storeLists as it contains your full data. What you can do is create a method as below in your adapter

 public void setList(ArrayList<StoreList> mList){
   this.storeList = mList;
   notifyDataSetChanged();
 }

And in your fragment once you have downloaded data and added to your storelist, call setList method as follows

 mAdapter.setList(storeList);

also remove mAdapter.notifyDataSetChanged() in your storeReq method as it does nothing.

Upvotes: 1

Related Questions