Recyclerview不会在searchview文本更改时更新

时间:2019-04-04 11:01:20

标签: android android-recyclerview adapter searchview notifydatasetchanged

朋友,我有一个edittext,它的作用就像一个searchview。在文本更改时,它正在获取数据并存储在filterdNames中,但是它没有更新recyclerview。请帮助,我是Andriod的新手。 这是我的recyclerview适配器代码-

public class MyCategoryAdaptercheckbox extends RecyclerView.Adapter<MyCategoryAdaptercheckbox.ViewHolder> {
    List<GetMyCategoryAdapter> getMyCategoryAdapter;
    Context context;
    List<String> category_name;
   GetMyCategoryAdapter getMyCategoryAdapter1;

    public MyCategoryAdaptercheckbox(List<GetMyCategoryAdapter> getMyCategoryAdapter, Context context, List<String> category_name) {
    this.getMyCategoryAdapter = getMyCategoryAdapter;
    this.context = context;
    this.category_name = category_name;
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (viewHolder instanceof ViewHolder){
        }
        getMyCategoryAdapter1 =  getMyCategoryAdapter.get(i);
      ((ViewHolder) viewHolder).tv_categorytitle.setText(getMyCategoryAdapter1.getC_name());
        ((ViewHolder) viewHolder).tv_categoryid.setText(getMyCategoryAdapter1.getC_id());
        ((ViewHolder) viewHolder).gt= getMyCategoryAdapter1;
    }

    public void filterList(ArrayList<String> filterdNames) {
       this.category_name = filterdNames;
       notifyDataSetChanged();
    } 
}

这是我的GetMyCategoryAdapter类代码-

public class GetMyCategoryAdapter {
    String c_name,c_id;

    public String getC_name() {
        return c_name;
    }

    public void setC_name(String c_name) {
        this.c_name = c_name;
    }

    public String getC_id() {
        return c_id;
    }

    public void setC_id(String c_id) {
        this.c_id = c_id;
    }
}

这是片段代码-

searchView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //after the change calling the method and passing the search input
                filter(editable.toString());
            }
        });
   private void filter(String text) {
        //new array list that will hold the filtered data
        ArrayList<String> filterdNames = new ArrayList<>();

        //looping through existing elements
        for (String s : category_name) {
            //if the existing elements contains the search input
            if (s.toLowerCase().contains(text.toLowerCase())) {
                //adding the element to filtered list
                filterdNames.add(s);
            }
        }

        //calling a method of the adapter class and passing the filtered list
        ((MyCategoryAdaptercheckbox) MyAdapter).filterList(filterdNames);



    }

1 个答案:

答案 0 :(得分:0)

问题出在filterList方法中: 您更新了category_name,但没有更新getMyCategoryAdapter(在onBindViewHolder中使用)。尝试在getMyCategoryAdapter之前更改notifyDataSetChanged