2016-02-28 66 views
0

我的Android应用程序中有一个recyclerview,列出了一些带有复选框的记录。通过选中复选框选择记录时,我发现一旦我检查了记录,来自recyclerview的其他记录就会自动被选中。有时以前选中的复选框会自动取消选中。我试过 “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” 其他解决方案但是这并不能解决我的问题。在android中检查recyclerview中的复选框后,其他随机复选框会自动检查

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

     } 
    } 
} 

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

回答

0

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

清单项目被删除并重新使用—如果您删除了视图中已选中的框,附加到滚动另一端的新视图也将检查它,因为您只更改回调中的leadCheckBox状态。

你应该总是用他们当前的状态初始化你的视图。如果它被检查,检查它,如果不是,则不要。

因此,添加viewHolder.leadCheckBox.setChecked(isItemChecked);onBindViewHolder,它会工作。您将不得不取代isItemChecked与您的逻辑检索在该位置的项目的实际状态...

+0

当然,我会尝试它,让你知道如果任何问题仍然存在...谢谢 –