我正在使用具有复选框和搜索过滤器的recyclerview。当我在recyclerview上使用过滤器进行搜索时,我们就开始了。例如,我在回收者视图(A,B,C)上有3个项目。我第一次检查项目A,然后我搜索项目B和检查项目B,当我删除搜索查询并返回至recyclerview(使用notifydatasetchanged()刷新recyclerview)时,没有任何错误,如此
但当我取消选中这两个项目(项目A和B),然后我搜索项目c并检查项目c时,检查的第一项项目(在此处为A)始终检查但是在checkarraylist上没有项目A.
这是recyclerview中列表的模型
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刷新所有未检查的项目时,即使它在checkitems数组列表中为null也会被检查。如果我没有使用搜索它工作正常
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);
}
}
});