我的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編碼新手,請幫我解決我的問題。謝謝
當然,我會嘗試它,讓你知道如果任何問題仍然存在...謝謝 –