搜索时RecyclerView Filter方法中的错误

时间:2017-10-28 10:57:28

标签: android android-recyclerview

import android.content.Context;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Filter;

import android.widget.Filterable;

import android.widget.TextView;



public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myviewholder> 

implements Filterable

 {


    private Context context;

    private User[] data;


    public MyAdapter(Context context, User[] data)
 {
        this.context = context;

        this.data = data;


    }

    @Override

    public myviewholder onCreateViewHolder(ViewGroup parent, int viewType) 
{

        LayoutInflater Inflater = LayoutInflater.from(parent.getContext());

        View view = Inflater.inflate(R.layout.items, parent, false);

        return new myviewholder(view);

    }

    @Override

    public void onBindViewHolder(myviewholder holder, int position) {



         final User user = data[position];

        holder.t1.setText(user.getName());

        holder.t2.setText(user.getNumber());



    }

    @Override
    public int getItemCount() {

        return data.length;

    }


    @Override
    public Filter getFilter()

    {
        return new Filter() {

            @Override

            protected FilterResults performFiltering(CharSequence charSequence)
 {

                String charString = charSequence.toString();


                if (charString.isEmpty()) {


                    data = data;

                } else {


                    ArrayList<data> filteredList = new ArrayList<>();


                    for (data androidVersion : mArrayList) {


                        if (data.getName().toLowerCase().contains(charString) || androidVersion.getNumber().toLowerCase().contains(charString)) 
{


                            filteredList.add(data);

                        }

                    }

                    mFilteredList = filteredList;

                }


                FilterResults filterResults = new FilterResults();

                filterResults.values = mFilteredList;

                return filterResults;

            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults)
 {
                mFilteredList = (ArrayList<data>) filterResults.values;

                notifyDataSetChanged();

            }

        };

    }

    public class myviewholder extends RecyclerView.ViewHolder

    {

         TextView t1;

         TextView t2;


        public myviewholder(View itemview) {

            super(itemview);


            t1=(TextView) itemview.findViewById(R.id.t1);

            t2=(TextView) itemview.findViewById(R.id.t2);




        }

    }



}

1 个答案:

答案 0 :(得分:0)

我在下面做过。这段代码可以帮到你。

在适配器中添加新功能以更新列表

public void updateList(List<classHolder> list){
 displayedList = list;
 notifyDataSetChanged(); }

添加textWatcher进行搜索,假设您使用Edittext作为搜索字段。

searchField.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) {

        // filter your list from your input
        filter(s.toString());
        //you can use runnable postDelayed like 500 ms to delay search text
    }
});

void filter(String text){
 List<classHolder> temp = new ArrayList();
 for(classHolder d: displayedList){
       //or use .equal(text) with you want equal match
       //use .toLowerCase() for better matches
       if(d.getEnglish().contains(text)){
           temp.add(d);
       }
 }
 //update recyclerview
 disp_adapter.updateList(temp);
}