在Android的回收器视图中选中复选框后,其他随机复选框会自动选中



我的Android应用程序中有一个回收器视图,列出了一些带有复选框的记录。在通过选中复选框选择记录时,我发现一旦我检查了记录,回收器视图中的其他记录就会自动被选中。有时,先前选择的复选框会自动取消选中。我尝试了其他解决方案"如何检查jQuery中是否选中了复选框?"选中列表视图中的复选框也会选中其他随机复选框","安卓回收器查看复选框随机检查" 但这并不能解决我的问题。

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

这是我的适配器代码。我是安卓编码的新手,请帮助我解决我的问题。谢谢

您正在使用RecyclerView .您遇到的功能是回收部分。

列表项将被删除并重复使用 - 如果您选中了视图中的复选框,则在滚动时附加到另一端的新视图也将选中该框,因为您只更改回调中leadCheckBox的状态。

始终使用视图的当前状态初始化视图。如果检查,请检查它,如果不是,请不要检查。

因此,将viewHolder.leadCheckBox.setChecked(isItemChecked);添加到onBindViewHolder中,它将起作用。您将不得不将isItemChecked替换为检索该位置项目的实际状态的逻辑......

相关内容

最新更新