列表视图中的复选框在滚动时自动检查和取消选中

时间:2017-11-08 04:28:54

标签: android listview checkbox unchecked

我知道这个问题一再被问到,但我仍然没有找到/理解我发现的任何内容。每当列表向下滚动时,都会取消选中该复选框,或者只要向上滚动列表,就会选中一些复选框。

这是我的代码:

@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
    //View v = View.inflate(mContext, R.layout.sales_invoice_custom,null);
    final ViewHolder holder;

    if (view == null) {
        view = View.inflate(mContext, R.layout.sales_invoice_custom, null);
        holder = new ViewHolder();

        holder.SelectInvoiceCB = (CheckBox) view.findViewById(R.id.selectInvoiceCB);
        holder.SalesInvoiceNo = (TextView) view.findViewById(R.id.SINo);
        holder.InvoiceDate = (TextView) view.findViewById(R.id.SIDate);
        holder.InvoiceAmount = (TextView) view.findViewById(R.id.SIAmount);
        holder.AmountDue = (TextView) view.findViewById(R.id.SIAmountDue);
        holder.DueDate = (TextView) view.findViewById(R.id.SIdueDate);
        holder.PayFull = (CheckBox) view.findViewById(R.id.SIFull);
        holder.PayPartial = (CheckBox) view.findViewById(R.id.SIPartial);
        holder.TotalAmount = (EditText) view.findViewById(R.id.SITotalAmount);
        holder.CreditMemoID = (TextView) view.findViewById(R.id.creditMemoID);
        holder.CreditMemoDate = (TextView) view.findViewById(R.id.creditMemoDate);
        holder.CreditMemoReason = (TextView) view.findViewById(R.id.creditMemoReason);
        holder.LL2 = (LinearLayout) view.findViewById(R.id.ll2);
        holder.LL3 = (LinearLayout) view.findViewById(R.id.ll3);

        view.setTag(holder);

    } else {
        holder = (ViewHolder) view.getTag();
    }

    InvoicePopulate(holder,position);

    return view;
}


public void InvoicePopulate(final ViewHolder holder, final int position) {
    dbHelper = new DBHelper(mContext);

    Calendar c = Calendar.getInstance();
    SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
    final String formattedDate = df1.format(c.getTime());


            //holder.SelectInvoiceCB.setChecked(false);
            holder.SalesInvoiceNo.setText(invoiceLists.get(position).getSales_Invoice_ID());
            holder.InvoiceDate.setText(invoiceLists.get(position).getInvoice_Date());
            holder.DueDate.setText(invoiceLists.get(position).getDue_Date());

            float invAmount = 0;
            invAmount = Math.round(Float.parseFloat(invoiceLists.get(position).getInvoice_Amount())*100.00)/(float)100.00;
            holder.InvoiceAmount.setText(String.format("%,.2f",invAmount));
            holder.AmountDue.setText(String.format("%,.2f",invAmount));


            try {
                if (invoiceLists.get(position).getAmount_Paid().toString().equals("") ||
                        invoiceLists.get(position).getAmount_Paid().toString().equals("0")) {
                    invAmount = 0;
                    invAmountDue = 0;
                    invAmountPaid = 0;

                    invAmount = Math.round(Float.parseFloat(invoiceLists.get(position).getInvoice_Amount())*100.00)/(float)100.00;
                    invAmountDue = invAmount - invAmountPaid;
                    Log.e("Without AmountPaid ", "Amount Due : " + String.valueOf(invAmountDue));
                } else {
                    invAmount = 0;
                    invAmountDue = 0;
                    invAmountPaid = Math.round(Float.parseFloat(invoiceLists.get(position).getAmount_Paid())*100.00)/(float)100.00;
                    invAmount = Math.round(Float.parseFloat(invoiceLists.get(position).getInvoice_Amount())*100.00)/(float)100.00;
                    invAmountDue = invAmount - invAmountPaid;
                    Log.e("With AmountPaid ", "Amount Due : " + String.valueOf(invAmountDue));
                }

                final float finalInvAmount = invAmountDue;
                holder.PayFull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (holder.PayFull.isChecked()) {
                            holder.PayPartial.setChecked(false);
                            if (holder.SelectInvoiceCB.isChecked()) {
                                invoiceStatusValue = "PAID_FULL";
                                holder.TotalAmount.setText(String.valueOf(Math.round(finalInvAmount*100.00)/100.00));
                                //holder.TotalAmount.setText(holder.InvoiceAmount.getText().toString());
                            }
                        }
                    }
                });

                holder.PayPartial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (holder.PayPartial.isChecked()) {
                            holder.PayFull.setChecked(false);
                            if (holder.SelectInvoiceCB.isChecked()) {
                                invoiceStatusValue = "PAID_PARTIAL";
                                holder.TotalAmount.setText("0.00");
                            }
                        }
                    }
                });

                holder.AmountDue.setText(String.format("%,.2f",invAmountDue));
            } catch (Exception e) {
                e.getMessage();
            }

            if (TotalPaymentAmount >= Float.parseFloat(String.valueOf(invAmountDue))) {
                holder.SelectInvoiceCB.setChecked(true);
                holder.PayFull.setChecked(true);
                holder.PayFull.setClickable(true);
                holder.PayPartial.setClickable(true);

                holder.TotalAmount.setText(String.valueOf(Math.round(invAmountDue*100.00)/100.00));
            }

        }


    } catch (Exception e){
        e.getMessage();
        System.out.println("Error - " + e);
    } finally {
        dbHelper.close();
    }
}

3 个答案:

答案 0 :(得分:0)

您应该在viewmodel class.Ex中选中是否存储复选框。 boolean isChecked;比在getView()中设置了viewmodel的复选框。

答案 1 :(得分:0)

您必须在模型类中管理已检查状态。在您的情况下,您必须为PayFull,PayPartial和SelectInvoiceCB复选框选中状态管理三个布尔值。 当你完成setChecked时,更新模型类检查的行为,如:

    class YourModel{
              public boolean isPayFull, isPayPartial, isSelectInvoice;
        }

        //This will update UI from model check behaviour
        holder.PayFull.setChecked(invoiceLists.get(position).isPayFull);
        holder.PayPartial.setChecked(invoiceLists.get(position).isPayPartial);
        holder.SelectInvoiceCB.setChecked(invoiceLists.get(position).isSelectInvoice);    
        holder.PayFull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (holder.PayFull.isChecked()) {
                                    holder.PayPartial.setChecked(false);
                                    if (holder.SelectInvoiceCB.isChecked()) {
                                        invoiceStatusValue = "PAID_FULL";

                                    }
                                }
    //This is to manage the state of checkbox in model
    invoiceLists.get(position).isPayFull = holder.PayFull.isChecked();
    invoiceLists.get(position).isPayPartial= holder.PayPartial.isChecked();
    invoiceLists.get(position).isSelectInvoice= holder.SelectInvoiceCB.isChecked();
                            }
                        });

                        holder.PayPartial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (holder.PayPartial.isChecked()) {
                                    holder.PayFull.setChecked(false);
                                    if (holder.SelectInvoiceCB.isChecked()) {
                                        invoiceStatusValue = "PAID_PARTIAL";
                                        holder.TotalAmount.setText("0.00");
                                    }
                                }
    //This is to manage the state of checkbox in model
    invoiceLists.get(position).isPayFull = holder.PayFull.isChecked();
    invoiceLists.get(position).isPayPartial= holder.PayPartial.isChecked();
    invoiceLists.get(position).isSelectInvoice= holder.SelectInvoiceCB.isChecked();
                            }
                        });

通常,我更喜欢在复选框上单击事件而不是在列表项中检查更改侦听器,选中的更改将在滚动时调用并在每个滚动上更新UI以便更好地在复选框上应用单击事件并从模型管理已检查状态我提到你的课程。

答案 2 :(得分:0)

在回答你的问题时,它检查和取消选中的原因在于你所有CheckChangedListeners中的代码

if (holder.PayFull.isChecked()) {
      holder.PayPartial.setChecked(false);

选中后,您将取消选中该复选框。

在单独的注释中,您需要在视图回收期间处理已检查的状态。 其他方面,复选框将无法保持正确的状态。滚动时。

一种选择是将ArrayList中的状态与数据一起保存。保留3个布尔变量state1,state2,state3作为复选框。

在InvoicePopulate(...)方法中 -

holder.PayFull.setChecked(invoiceLists.get(position).getState1())  

在检查已更改的侦听器中添加以下行

invoicelists.get(位置).setState1(器isChecked)

相关问题