Home > database >  Items don't reappear when removing search filter pattern Cardview
Items don't reappear when removing search filter pattern Cardview

Time:09-08

I tried adding a search bar that makes the items disappear when they aren't being searched, and only items that contain the character that are typed in the search are left.

The search works fine, but when I'm backspacing (like if I made a typo or wanted to search something else) the items don't reappear. I tried playing with the variables. I followed the guide of https://www.youtube.com/watch?v=sJ-Z9G0SDhc .

package com.example.scannerapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements Filterable {

    Context context;
    private ArrayList<ItemModal> itemModalArrayList;
    private ArrayList<ItemModal> fullItemList;

    public MyAdapter(Context context, ArrayList<ItemModal> itemModalArrayList) {
        this.context = context;
        this.itemModalArrayList = itemModalArrayList;
        this.fullItemList = new ArrayList<>(itemModalArrayList);
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.activity_show_db,parent,false);

        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
        ItemModal item = itemModalArrayList.get(position);

        holder.itemName.setText(item.name);
        holder.itemPrice.setText(item.price);
        holder.itemCode.setText(item.code);
    }

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

    @Override
    public Filter getFilter() {
        return itemFilter;
    }
    private Filter itemFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            List<ItemModal> filteredList = new ArrayList<>();
            if(charSequence == null || charSequence.length()==0){
                filteredList.addAll(itemModalArrayList);

            }
            else{
                String filterPattern = charSequence.toString().toLowerCase().trim();
                for (ItemModal itemModal : itemModalArrayList){
                    if(itemModal.getName().toLowerCase().contains(filterPattern)){
                        filteredList.add(itemModal);
                    }
                }
            }
            FilterResults results = new FilterResults();
            results.values = filteredList;
            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            itemModalArrayList.clear();
            itemModalArrayList.addAll((List) filterResults.values);
            notifyDataSetChanged();

        }
    };

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView itemName, itemPrice, itemCode;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            itemName = itemView.findViewById(R.id.tvItemName);
            itemPrice = itemView.findViewById(R.id.tvItemPrice);
            itemCode = itemView.findViewById(R.id.tvItemCode);
        }
    }

}

And here is the OnCreate method of the search bar in ViewItems.class

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search,menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            myAdapter.getFilter().filter(s);
            return false;
        }
    });
    return true;
}

CodePudding user response:

You can use Text Watcher in Search Field and put notifyDataSetChanged() when the edit text is empty

CodePudding user response:

searchView.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub
    }

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

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {
        String str = s.getText().toString();
        myAdapter.getFilter().filter(str);
        // here notifyDataSetChanged();
    }
});
  • Related