我正在将recyclerview与复选框和搜索过滤器一起使用。当我在recyclerview上使用过滤器搜索时,问题开始了。例如,我在回收者视图中有3个项目(A,B,C)。第一次我检查项目A,然后我搜索项目B并检查项目B,并且当我删除搜索查询并返回到recyclerview(使用notifydatasetchanged()刷新recyclerview)时,没有什么不对的
但是当我取消选中两个项目(项目A和B),然后我搜索项目c并检查项目c时,总是检查了第一个项目(在这里A),但是checkedarraylist上没有项目A
这是rectclerview中列表的模型
public class herbsModel {
private boolean isSelected;
private String idHerbs,nameHerbs;
public herbsModel(String idHerbs, String nameHerbs) {
this.idHerbs = idHerbs;
this.nameHerbs = nameHerbs;
}
public String getIdHerbs() {
return idHerbs;
}
public String getNameHerbs() {
return nameHerbs;
}
public boolean getSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
这是对来自mainactivity的edittext的搜索
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
private void filter(String s) {
ArrayList<herbsModel> filteredlist = new ArrayList<>();
for (herbsModel item : herbsModels)
{
if(item.getNameHerbs().toLowerCase().contains(s.toLowerCase()))
{
filteredlist.add(item);
}
}
adapter.filterlist(filteredlist);
}
这是检查已检查项目的方法
sb = new StringBuffer();
for (herbsModel h : adapter.checkedHerbs)
{
sb.append(h.getNameHerbs());
sb.append("\n");
}
if (adapter.checkedHerbs.size()>0)
{
Toast.makeText(getActivity(),sb.toString(),Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getActivity(),"Please Check Plants",Toast.LENGTH_SHORT).show();
}
这是适配器上的onbindviewholder,它使用逻辑来获取已检查或未检查的项目并将已检查的项目存储到新数组并设置已检查的项目
@Override
public void onBindViewHolder(@NonNull final herbsPredictViewHolder herbsPredictViewHolder, int i) {
final herbsModel detailherbs = herbsModelList.get(i);
herbsPredictViewHolder.name.setText(detailherbs.getNameHerbs());
if(detailherbs.getSelected()==true)
{
herbsPredictViewHolder.checkBox.setChecked(true);
}
else{
herbsPredictViewHolder.checkBox.setChecked(false);
herbsPredictViewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
CheckBox checkBox = (CheckBox) v;
if (checkBox.isChecked())
{
checkedHerbs.add(herbsModelList.get(pos));
detailherbs.setSelected(true);
}
else if(!checkBox.isChecked())
{
checkedHerbs.remove(herbsModelList.get(pos));
detailherbs.setSelected(false);
}
}
});
}
}
这是适配器上的过滤器功能
public void filterlist (ArrayList<herbsModel> filteredList)
{
herbsModelList =filteredList;
notifyDataSetChanged();
}
问题是从searchview中检查了某些内容之后开始的,然后在recyclerview刷新时,我所有未选中的项目都被选中了,即使在checkeditems数组列表中为null也是如此。如果我不使用搜索,那就很好
答案 0 :(得分:0)
CheckBox checkBox = (CheckBox) v;
if (checkBox.isChecked())
{
checkedHerbs.add(herbsModelList.get(pos));
detailherbs.setSelected(true);
}
else if(!checkBox.isChecked())
{
checkedHerbs.remove(herbsModelList.get(pos));
detailherbs.setSelected(false);
}
我认为您需要对此进行更新
if (!detailherbs.getSelected())
{
checkedHerbs.add(herbsModelList.get(pos));
detailherbs.setSelected(true);
}
else
{
checkedHerbs.remove(herbsModelList.get(pos));
detailherbs.setSelected(false);
}
并将您的其他代码更新为
else
herbsPredictViewHolder.checkBox.setChecked(false);
herbsPredictViewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
CheckBox checkBox = (CheckBox) v;
if (checkBox.isChecked())
{
checkedHerbs.add(herbsModelList.get(pos));
detailherbs.setSelected(true);
}
else if(!checkBox.isChecked())
{
checkedHerbs.remove(herbsModelList.get(pos));
detailherbs.setSelected(false);
}
}
});