searchview EditText with Recyclerview

android android-recyclerview android-edittext searchview

940 观看

1回复

61 作者的声誉

I am trying to add search functionality in my recyclerview. I found a tutorial on google and implemented the same. Search using editText in recyclerview. But my list disappears when I enter the input. Where am I going wrong? My recycler view contains multiple textboxes. Below is MyAdapter class.

How do I implement search functionality with the below code?

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>implements Filterable {
private List<ListItem> listItems;
private List<ListItem> mFilterData;
private Context context;
String m,e;
private ItemFilter mFilter = new ItemFilter();
public MyAdapter(List<ListItem> itemList, Context context) {
    this.listItems = itemList;
    this.context = context;
    this.mFilterData = listItems;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ListItem listItem = mFilterData.get(position);
    holder.nameText.setText(listItem.getName());

    holder.addText.setText(listItem.getAddress());
    Picasso.with(context).load(listItem.getLogo()).resize(180,80).into(holder.imageText);

}

@Override
public int getItemCount() {
    return mFilterData.size();
}
public int getCount() {
    return mFilterData.size();
}
public ListItem getItem(int position) {
    return mFilterData.get(position);
}

public long getItemId(int position) {
    return position;
}

@Override
public Filter getFilter() {
    return mFilter;
}
private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        String userString = constraint.toString().toLowerCase();
        FilterResults filterResults = new FilterResults();
        final List<ListItem> originalList = listItems;
        int count = originalList.size();
        final ArrayList<ListItem> resultList = new ArrayList<>(count);
        ListItem tDescription;
        for (int i = 0; i < count; i++) {
          tDescription= originalList.get(i);
            if (tDescription.getName().toLowerCase().contains(userString)) {
                resultList.add(tDescription);
            }
        }

        filterResults.values = resultList;
        filterResults.count = resultList.size();

        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mFilterData = (ArrayList<ListItem>) results.values;
        notifyDataSetChanged();
   }
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView nameText, addText,mobText,emailText;
    public ImageView imageText;

    public ViewHolder(View itemView) {
        super(itemView);

   //code
    }
  }
}

SEARCH

 private void addTextListener() {
    search.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {

        }

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

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            s = s.toString().toLowerCase();

            final List<ListItem> filteredList = new ArrayList<>();

            for (int i = 0; i < listItems.size(); i++) {

                final String text = listItems.get(i).toString();
                if (text.contains(s)) {

                    filteredList.add(listItems.get(i));
                }
            }
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            adapter = new MyAdapter(filteredList, getActivity());
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();  // data set changed

        }
    });
}
作者: Aparna Mutnalkar 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

2249 作者的声誉

Try below code

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
     String userString = constraint.toString().toLowerCase();
     if (userString.isEmpty()) {
        mFilterData = listItems;
     } else {
         ArrayList<ListItem> resultList = new ArrayList<>();
         for (ListItem mListItem : listItems) {
             if (mListItem.getName().toLowerCase().contains(userString)) {
                   resultList.add(mListItem);
             }
         }

         mFilterData = resultList;
       }

         FilterResults filterResults = new FilterResults();
         filterResults.values = mFilterData;
         filterResults.count = mFilterData.size();
         return filterResults;
     }
}

Updated

Remove all code from edittext OnTextChange and write below code,no need to set adapter and all just write below oneline code and remove rest of code

adapter.getFilter().filter(s.toString());
作者: Munir 发布者: 2017 年 12 月 27 日
32x32