Skip to content Skip to sidebar Skip to footer

Android: How Do I Maintain The View For Cardviews In A Recyclerview List?

I have a RecyclerView list that shows a vertical list of CardViews. The AppBar has an action_search MenuItem that handles a SearchView method when clicked on. The SearchView runs

Solution 1:

try this code.. set this method in adapter

publicvoidsetFilter(ArrayList<ListItem> listItem) {
        listItems = newArrayList<>();
        listItems.addAll(listItem);
        notifyDataSetChanged();
    }

and use like this in onQueryTextChange()

final ArrayList<ListItem> filteredModelList =filter(listItems, newText);
adapter.setFilter(filteredModelList);

and this is the filter method.

privateArrayList<ListItem> filter(ArrayList<ListItem> models, String query) {
        query = query.toLowerCase();

        finalArrayList<ListItem> filteredModelList = new ArrayList<>();
        for (ListItem model : models) {
            finalString text = model.getTodo().toLowerCase();
            if (text.contains(query)) {
                filteredModelList.add(model);
            }
        }

        return filteredModelList;
    }

and remove last two lines from your adapter constructor. with this code you dont have to create second array list object for filter, so you dont need to clear your arraylist object.. it works good when you write or clear text in searchview.

Post a Comment for "Android: How Do I Maintain The View For Cardviews In A Recyclerview List?"