samedi 16 février 2019

The first checked item always checked when refresh recyclerview

I am using recyclerview with checkbox and search filter. The truble start when i search using filter on recyclerview. for example i have 3 items on recycler view (A,B,C). first time i check item A , and then i search for item B and Check Item B and when i delete the query on search and back to recyclerview (with notifydatasetchanged() to refresh the recyclerview) there is nothing wrong like this

checked

but when i uncheck both items (items A and B), and then i search item c and check the item c, THE FIRST ITEM CHECKED (in here A ) always checked but there is no item A on checkedarraylist

checked but no exist

this is the model for the list in 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;
    }
}

this is the search from edittext on mainactivity

    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);
    }

this is how to check the checked items

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();
                }

this is the onbindviewholder on adapter that use logic to get checked or unchecked item and store checked item to new array and set checked the items

  @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);
                }

            }
        });
    }



}

this is the filter function on adapter

public void filterlist (ArrayList<herbsModel> filteredList)
    {
        herbsModelList =filteredList;
        notifyDataSetChanged();

    }




Aucun commentaire:

Enregistrer un commentaire