Setelah mencentang kotak di recyclerview di Android, kotak centang acak lainnya dicentang secara otomatis

Saya memiliki satu tampilan pendaur ulang di aplikasi Android saya, mencantumkan beberapa catatan dengan kotak centang. Saat memilih catatan dengan mencentang kotak, saya menemukan bahwa setelah saya mencentang catatan, catatan lain dari recyclerview secara otomatis dipilih. Dan terkadang kotak centang yang dipilih sebelumnya tidak dipilih secara otomatis. Saya mencoba solusi lain dari "Bagaimana cara memeriksa apakah kotak centang dicentang di jQuery?", "Mencentang kotak di tampilan daftar akan membuat kotak centang acak lainnya diperiksa juga", "Kotak Centang Android RecyclerView memeriksa secara acak" Tapi ini tidak tidak menyelesaikan masalahku.

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

       }
   }
}

Ini adalah kode saya untuk adaptor. Saya baru dalam pengkodean Android, Tolong bantu saya untuk menyelesaikan masalah saya. Terima kasih


person Sanket Ranaware    schedule 28.02.2016    source sumber


Jawaban (1)


Anda menggunakan RecyclerView. Fitur yang Anda temui adalah bagian daur ulang.

Item daftar akan dihapus dan digunakan kembali jika Anda telah menghapus kotak centang pada tampilan, tampilan baru yang dilampirkan ke ujung lain pada gulir juga akan mencentangnya karena Anda hanya mengubah status leadCheckBox dalam panggilan balik.

Anda harus selalu menginisialisasi tampilan Anda dengan statusnya saat ini. Kalau dicentang ya dicek, kalau tidak ya jangan.

Jadi tambahkan viewHolder.leadCheckBox.setChecked(isItemChecked); ke onBindViewHolder dan itu akan berhasil. Anda harus mengganti isItemChecked dengan logika Anda untuk mengambil status sebenarnya untuk item di posisi...

person David Medenjak    schedule 28.02.2016
comment
Tentu saya akan mencobanya, dan memberi tahu Anda jika masih ada masalah...Terima kasih - person Sanket Ranaware; 29.02.2016