0

I have one recyclerview in my android application, listing some records with checkboxes. While selecting records by checking checkboxes, I found that once I checked records, other records from recyclerview automatically get selected. And sometimes the previously selected checkbox get unselected automatically. I tried the other solutions from "How to check whether a checkbox is checked in jQuery?", "Checking a checkbox in listview makes other random checkboxes checked too", "Android RecyclerView Checkbox randomly checks" But this doesn't solve my issue.

public class LeadListAdapter extends RecyclerView.Adapter<LeadListAdapter.LeadListViewHolder> {

private List<Customer> customerDataList;
private Activity mActivity;
public static List<PEdgeLeads> selectedLeadsList;
public static List<Customer> selectedCustomerList;
private List<PEdgeLeads> leadDataList;
private int dataSize = 0;
public static Context mContextAdapter;

public LeadListAdapter(List<Customer> customerDetails, List<PEdgeLeads> leadDetails, Activity activity) {

    System.out.println("Inside LeadList Adapter Constructor");
    leadDataList = new ArrayList<PEdgeLeads>();
    customerDataList = new ArrayList<Customer>();
    selectedLeadsList = new ArrayList<PEdgeLeads>();
    selectedCustomerList = new ArrayList<Customer>();

    dataSize = leadDataList.size();
    System.out.println("lead list :: " + leadDataList.size() + " and customer data list :: " + customerDataList.size());

    this.customerDataList = customerDetails;
    this.leadDataList = leadDetails;
    System.out.println("Lead Details List :: " + this.leadDataList.get(0).getLeadName());
    System.out.println("Customer Details List :: " + this.customerDataList.get(0).getFirstName());
    this.mActivity = activity;
}

@Override
public LeadListAdapter.LeadListViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.lead_list_view, viewGroup, false);
    System.out.println("Inflating Lead List View NOW");

    return new LeadListViewHolder(itemView);
}

@Override
public void onBindViewHolder(final LeadListAdapter.LeadListViewHolder viewHolder, final int position) {

    viewHolder.leadName.setText(leadDataList.get(position).getLeadName());

    //if (customerDataList.size() < position) {

    if (!customerDataList.get(position).getContactNumber().equals("") || customerDataList.get(position).getContactNumber() != null) {
        final String mobileNumber = customerDataList.get(position).getContactNumber().toString().trim();

        viewHolder.leadMobile.setText(mobileNumber);
        System.out.println("Mobile NUmber about to get registered");

    boolean isChecked = viewHolder.isLeadChecked;

    viewHolder.leadCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                System.out.println("Lead Added to list");
                selectedCustomerList.add(customerDataList.get(position));
                selectedLeadsList.add(leadDataList.get(position));
                System.out.println("Selected Lead List :: " + selectedLeadsList.toString());
                System.out.println("Selected Customer List :: " + selectedCustomerList.toString());
            } else {
                System.out.println("Lead removed from list");
                selectedCustomerList.remove(customerDataList.get(position));
                selectedLeadsList.remove(leadDataList.get(position));
                System.out.println("Selected Lead List :: " + selectedLeadsList.toString());
                System.out.println("Selected Customer List :: " + selectedCustomerList.toString());
            }
        }
    });
}

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

public static class LeadListViewHolder extends RecyclerView.ViewHolder {

    protected TextView leadName;
    protected TextView leadEmail;
    protected TextView leadMobile;
    protected TextView redPopup;
    protected TextView greyPopup;
    protected CheckBox leadCheckBox;
    protected View verticalSeparator;
    protected boolean isLeadChecked;

    public LeadListViewHolder(View v) {
        super(v);

        leadName = (TextView) v.findViewById(R.id.lead_name);

        leadEmail = (TextView) v.findViewById(R.id.lead_email);

        leadMobile = (TextView) v.findViewById(R.id.lead_mobile);

        redPopup = (TextView) v.findViewById(R.id.red_popup);

        greyPopup = (TextView) v.findViewById(R.id.grey_popup);

        leadCheckBox = (CheckBox) v.findViewById(R.id.lead_checkbox);

        verticalSeparator = v.findViewById(R.id.separator);

        mContextAdapter = v.getContext();

        isLeadChecked = false;

        System.out.println("Got Lead Name's Id and handle");

       }
   }
}

This is my code for adapter. I am new in android coding, Please help me to solve my issue. Thank you

Community
  • 1
  • 1
Sanket Ranaware
  • 603
  • 2
  • 6
  • 13

1 Answers1

0

You are using a RecyclerView. The feature you have encountered is the recycling part.

List items get removed and reused—if you had the box checked in the view removed, the new view attached to the other end on scroll will also have it checked since you only change the state of leadCheckBox in the callback.

You should always initialize your views with their current state. If it is checked, check it, if it is not, don't.

So add viewHolder.leadCheckBox.setChecked(isItemChecked); to onBindViewHolder and it will work. You will have to replace isItemChecked with your logic of retrieving the actual state for the item at the position...

David Medenjak
  • 33,993
  • 14
  • 106
  • 134