0

I've facing a problem using checkbox with recycler view. The issue is that when I check the 2nd element then the element from bottom of the list also gets selected and similar it is happening when I select any other element. I've searched stackoverflow and found similar questions with answers and I've tried those solutions but they didn't worked.

Adapter Class:

public class addServicesCheckOutAdapter extends RecyclerView.Adapter<addServicesCheckOutAdapter.ViewHolder>  implements Filterable {

private List<ServiceList> listitemS;
private  List<ServiceList> listitemsFilter;
private Context context;

private Map<String,String> hashMap;
private ArrayList<String> pricesList;
private List<String> list;
private int prevSelection = -1;
private int selectedPosition;
private boolean var = false;
Set<Integer> mCheckedItemPositions = new HashSet<>();


public addServicesCheckOutAdapter(List<ServiceList> listitems, Context context) {
    this.listitemS = listitems;
    this.context = context;
    this.listitemsFilter = listitems;
}

public  void filteredList(List<ServiceList> filetList) {
    listitemS = filetList;
    notifyDataSetChanged();
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view  = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_add_services,parent,false);

    pricesList = new ArrayList<>();
    hashMap = new ConcurrentHashMap<>();
    list = new ArrayList<>();


    global.addServicesPrices = pricesList;
    global.addServicesNameAndID = hashMap;

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {

    final ServiceList listitem = listitemsFilter.get(position);
    holder.tvServiceName.setText(listitem.getService_name());
}

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

@Override
public Filter getFilter() {
    return  new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            String Key = constraint.toString();

            if (Key.isEmpty()){
                listitemsFilter = listitemS;
            }
            else {
                List<Listitem> IstFilter = new ArrayList<>();
            }
            FilterResults  filterResults = new FilterResults();
            filterResults.values= listitemsFilter;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            notifyDataSetChanged();
        }
    };
}

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

public static class ViewHolder extends RecyclerView.ViewHolder{

    TextView tvServiceName;
    CheckBox checkBox;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        tvServiceName = itemView.findViewById(R.id.tvServiceName);
        checkBox = itemView.findViewById(R.id.checkBox);
    }




}//View Holder

}

Umar Saleem
  • 109
  • 10
  • For every item in recyclerview define a separate checkbox – MMG May 06 '20 at 10:53
  • How can I do that? A link to a tutorial or some code would be really helpful – Umar Saleem May 06 '20 at 10:54
  • https://stackoverflow.com/questions/32427889/checkbox-in-recyclerview-keeps-on-checking-different-items – MMG May 06 '20 at 10:58
  • 1
    Does this answer your question? [CheckBox in RecyclerView keeps on checking different items](https://stackoverflow.com/questions/32427889/checkbox-in-recyclerview-keeps-on-checking-different-items) – MMG May 06 '20 at 10:59
  • Yes, one of the solution worked for me. Thank you very much – Umar Saleem May 06 '20 at 11:12

2 Answers2

1

Set below methods in your adapter

@Override
 public long getItemId(int position) 
 {
        return position;
 }

 @Override
 public int getItemViewType(int position) 
 {
        return position;
 }

Hope this may help you.

Komal
  • 328
  • 3
  • 15
0

You are not setting checkbox checked state in onBindViewHolder. I assume, that mCheckedItemPositions should hold positions of checked items? Then add something like this:

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {

    final ServiceList listitem = listitemsFilter.get(position);
    holder.tvServiceName.setText(listitem.getService_name());
    if (mCheckedItemPositions.contains(position) {
        holder.checkBox.setChecked(true);
    } else {
        holder.checkBox.setChecked(false);
    }
}

You will also need to add checkbox onCheckedChanged listener into your viewholder and update mCheckedItemPositions. Something like this:

checkBox = itemView.findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            if (isChecked) {
                //add position to mCheckedItemPositions
            } else {
                //remove position from mCheckedItemPositions
            }
       }
   }
);  
Kubik
  • 610
  • 7
  • 15