2017-11-08 5 views
1

この質問は何度も繰り返し尋ねられていますが、まだ見つけられませんでした。リストがスクロールダウンされるたびにチェックボックスがオフになっている場合、またはリストがスクロールアップされたときにチェックボックスの一部がチェックされると、チェックボックスはオフになります。ここに私のコードがあります。これを解決する方法を提案してください。ありがとうございました。リストビュー内のチェックボックススクロール時のチェックとアンチェックが自動的に行われます

@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(); 
    } 
} 

いずれの提案も大きな助けになります。ありがとうございました!

+0

試して、次のリスナーが追加変更しましたリンク、これはあなたにとって有用かもしれません。 https://stackoverflow.com/questions/10895763/checkbox-unchecked-when-i-scroll-listview-on-android https://stackoverflow.com/questions/9309250/checkbox-gets-unchecked-on -scroll-in-a-custom-listview –

答えて

0

あなたのビューモールドクラスにチェックボックスがチェックされているかどうかチェックします。 boolean isChecked; getView()よりもviewmodel ischeckedからのチェックボックスを設定します。

0

モデルクラスでは、チェック状態を管理する必要があります。あなたのケースでは、PayFull、PayPartialおよびSelectInvoiceCBチェックボックスのチェック状態に3つのブール値を管理する必要があります。あなたはその時点で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を更新すると、チェックボックスでappyのクリックイベントをうまく処理でき、モデルクラスからチェック状態を管理することができます。あなたの質問への答えで

+0

こんにちは、 これは良いです!ありがとうございました。しかし、チェックボックスをオフにしてリストビューをスクロールすると、チェックされます。 –

0

、それがチェックし、あなたは、すぐにその確認としてチェックボックスをオフにしているすべてのCheckChangedListeners

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

にあなたのコードのこの部分で嘘をオフだ理由。

ビューリサイクル中にチェック状態を処理する必要があります。 他のチェックボックスは適切な状態を維持しません。スクロールするとき。

1つの選択肢は、データと共にArrayListの状態を保存することです。 3つのブール変数state1、state2、state3をチェックボックスに入れます。 InvoicePopulate(...)メソッドで

- チェックで

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

このラインから

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

関連する問題