无法在Recyclerview.Adapter的适配器类上解析getFilter()方法

时间:2018-03-02 06:36:44

标签: java android android-recyclerview searchview

我正在使用实现Filterable接口的RecyclerView.Adapter类创建RecylerView。我已经彻底检查了我的代码,但可以找到错误的原因。当我在SearcView的setOnQueryTextListener.onQueryTextChange方法中使用此适配器类时,它显示“无法解析getFilter()方法”。

以下是自定义RecyclerView.Adapter类的代码:

public class CUGAdapter extends RecyclerView.Adapter<CUGAdapter.CUGViewHolder> implements Filterable {
private ArrayList<Contact> CUG;
private ArrayList<Contact> filteredCUG;


public class CUGViewHolder extends RecyclerView.ViewHolder{
    public TextView emp_id;
    public TextView emp_name;
    public TextView emp_designation;
    public TextView emp_mobile;
    public View layout;

    public CUGViewHolder(View v) {
        super(v);
        this.layout = v;
        this.emp_id = (TextView) v.findViewById(R.id.txt_empid);
        this.emp_name = (TextView) v.findViewById(R.id.txt_name);
        this.emp_designation = (TextView) v.findViewById(R.id.txt_designation);
        this.emp_mobile = (TextView) v.findViewById(R.id.txt_mobile);

    }
}

public CUGAdapter(ArrayList<Contact> CUG) {
    this.CUG = CUG;
}

@Override
public CUGAdapter.CUGViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v = inflater.inflate(R.layout.listview_layout, parent, false);
    CUGViewHolder vh = new CUGViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(CUGViewHolder holder, int position) {
    Contact c = filteredCUG.get(position);
    holder.emp_mobile.setText(c.mobile);
    holder.emp_designation.setText(c.designation);
    holder.emp_name.setText(c.name);
    holder.emp_id.setText(c.empid);
}

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

@Override
public Filter getFilter() {
    return new Filter() {
        private ArrayList<Contact> filtered = new ArrayList<Contact>();
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            String charString = charSequence.toString().toLowerCase();
            filtered.clear();
            if(charString.isEmpty()){
                filtered = CUG;
                //filteredCUG = CUG;
            }
            else{
                for (Contact cug : CUG){
                    if( cug.name.toLowerCase().contains(charString) || cug.mobile.toLowerCase().contains(charString) || cug.empid.toLowerCase().contains(charString)){
                        filtered.add(cug);
                    }
                 }
                 //filteredCUG = filtered;
            }
            FilterResults filterResults = new FilterResults();

            filterResults.values = filtered;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            //filteredCUG.clear();
            filteredCUG = (ArrayList<Contact>) filterResults.values;
            notifyDataSetChanged();
        }
    };
}}

以下是searchview listner的代码:

public boolean onCreateOptionsMenu(Menu menu) {
    menu.findItem(R.id.app_bar_search).getActionView();

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);

    SearchView searchView = (SearchView) menu.findItem(R.id.app_bar_search).getActionView();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            adapter.getFilter().filter(newText);// Error occurs at this line
            return false;
        }
    });
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
     return super.onOptionsItemSelected(item);
}}

1 个答案:

答案 0 :(得分:2)

将适配器初始化更改为

 CUGAdapter adapter = new CUGAdapter(this.cuglist);