2015-05-29 74 views
-5

嗨我有一個自定義列表視圖與四個編輯文本字段。最初這些編輯文本是由來自sqlite的數據填充的。這些編輯文本包含整數值。有一個Textfield顯示來自編輯文本的所有這些值的總和。每次滾動我的列表時,總和都會改變。自定義列表視圖與編輯文本

公共類CustomAdapterListForCollection擴展ArrayAdapter {

static class ViewHolder 
{ 
    TextView strAccountNo,strCustName, strCurInt,strLoanBal,strSavingBal; 
    EditText strExtraInt,strSaving,strFine; 
    CheckBox StatusCheckBox; 
} 
private StatementBean[] statementBean_arr; 
Activity context; 
EditText txtTotalCur,txtTotalExtra,txtTotalSaving,txtTotalFine,txtTotalCol; 
String ExtrInt="",Saving="",Fine=""; 
int totCurInt=0,oldExtra=0,newExtra=0,oldSav=0,newSav=0,newFin=0,oldFin=0,totExtra=0,totSav=0,totFin=0,totCol=0,intCur,intExtra,intSaving,intFine; 
public CustomAdapterListForCollection(Activity context,StatementBean[] statementBean_arr,EditText et1 ,EditText et2 ,EditText et3,EditText et4,EditText et5) 
{ 
    super(context, R.layout.collectionrow, statementBean_arr); 
    this.context = context; 
    this.statementBean_arr=statementBean_arr; 
    txtTotalCur=et1; 
    txtTotalExtra=et2; 
    txtTotalSaving=et3; 
    txtTotalFine=et4; 
    txtTotalCol=et5; 
} 
@Override 
public View getView(int position, View rowView, ViewGroup parent) 
{ 
    final ViewHolder viewHolder; 
    if(rowView==null) 
    { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     viewHolder=new ViewHolder(); 
     rowView= inflater.inflate(R.layout.collectionrow, null, false); 

     viewHolder.strAccountNo=(TextView)rowView.findViewById(R.id.th_acntno); 
     viewHolder.strCustName = (TextView) rowView.findViewById(R.id.th_acntnm);  
     viewHolder.strCurInt = (TextView) rowView.findViewById(R.id.th_acntcur); 
     viewHolder.strExtraInt = (EditText) rowView.findViewById(R.id.th_acntextra); 
     viewHolder.strSaving = (EditText) rowView.findViewById(R.id.th_acntsaving); 
     viewHolder.strFine = (EditText) rowView.findViewById(R.id.th_fine); 
     viewHolder.strLoanBal = (TextView) rowView.findViewById(R.id.th_loanBal); 
     viewHolder.strSavingBal = (TextView) rowView.findViewById(R.id.th_saveBal); 
     viewHolder.StatusCheckBox=(CheckBox)rowView.findViewById(R.id.statusCheckBox); 



     viewHolder.strAccountNo.setText(statementBean_arr[position].getAccount_no()); 

     viewHolder.strCustName.setText(statementBean_arr[position].getCust_name()); 

     viewHolder.strCurInt.setText(""+statementBean_arr[position].getCur_int_amt()); 

     try 
     { 
      totCurInt= totCurInt+Integer.valueOf(viewHolder.strCurInt.getText().toString()); 

     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     totCol=totCurInt; 

     viewHolder.strExtraInt.setText(""+statementBean_arr[position].getExtra_int_amt()); 

     viewHolder.strSaving.setText(""+statementBean_arr[position].getSaving()); 

     viewHolder.strFine.setText(""+statementBean_arr[position].getFine()); 

     viewHolder.strLoanBal.setText(statementBean_arr[position].getLoan_bal()); 

     viewHolder.strSavingBal.setText(statementBean_arr[position].getSaving_bal()); 

     viewHolder.StatusCheckBox.setChecked(statementBean_arr[position].isCheckFlag()); 

     txtTotalCur.setText(""+totCurInt); 
     txtTotalCol.setText(""+totCol); 
     txtTotalExtra.setText(""+totExtra); 
     txtTotalFine.setText(""+totFin); 
     txtTotalSaving.setText(""+totSav); 
     rowView.setTag(viewHolder); 

     viewHolder.strExtraInt.addTextChangedListener(new TextWatcher() 
     { 
      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
      {} 
      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) 
      { 
       if(!(viewHolder.strExtraInt.getText().toString().equals(""))) 
       { 
        try 
        { 
         ExtrInt=viewHolder.strExtraInt.getText().toString().trim(); 
         oldExtra= Integer.valueOf(viewHolder.strExtraInt.getText().toString()); 
         Log.e("CustomAdapterListForCollection","totCol 1 "+totCol); 
         totCol=totCol-oldExtra; 
         Log.e("CustomAdapterListForCollection","totCol 2 "+totCol); 
         totExtra=totExtra-oldExtra; 
         txtTotalExtra.setText(""+totExtra); 
         txtTotalCol.setText(""+totCol); 
         Log.e("CustomAdapterListForCollection","totCol 3 "+totCol); 
         Log.e("CustomAdapterListForCollection","oldExtra "+oldExtra); 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        oldExtra=0; 
       } 
      } 
      @Override 
      public void afterTextChanged(Editable arg0) 
      { 
       if(!(viewHolder.strExtraInt.getText().toString().equals(""))) 
       { 
        try 
        { 

         ExtrInt =viewHolder.strExtraInt.getText().toString(); 
         newExtra =Integer.parseInt(viewHolder.strExtraInt.getText().toString()); 
         totExtra=totExtra+newExtra; 
         totCol=totCol+newExtra; 
         txtTotalExtra.setText(""+totExtra); 
         txtTotalCol.setText(""+totCol); 

        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        newExtra=0; 
       } 

      } 
     }); 


     viewHolder.strSaving.addTextChangedListener(new TextWatcher() 
     { 
      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
      {} 
      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) 
      { 
       if(!(viewHolder.strSaving.getText().toString().equals(""))) 
       { 
        try 
        { 
         oldSav=Integer.parseInt(viewHolder.strSaving.getText().toString()); 
         totCol=totCol-oldSav; 
         totSav=totSav-oldSav; 
         txtTotalSaving.setText(""+totSav); 
         txtTotalCol.setText(""+totCol); 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        oldSav=0; 
       } 
      } 
      @Override 
      public void afterTextChanged(Editable arg0) 
      { 
       if(!(viewHolder.strSaving.getText().toString().equals(""))) 
       { 
        try 
        { 

         Saving=viewHolder.strSaving.getText().toString(); 
         newSav=Integer.parseInt(viewHolder.strSaving.getText().toString()); 

         totSav=totSav+newSav; 
         totCol=totCol+newSav; 
         txtTotalSaving.setText(""+totSav); 
         txtTotalCol.setText(""+totCol); 

        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        newSav=0; 
       } 

      } 
     }); 


     viewHolder.strFine.addTextChangedListener(new TextWatcher() 
     { 
      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
      {} 
      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
      { 

       if(!(viewHolder.strFine.getText().toString().equals(""))) 
       { 
        try 
        { 
         oldFin=Integer.parseInt(viewHolder.strFine.getText().toString()); 

         totCol=totCol-oldFin; 
         totFin=totFin-oldFin; 
         txtTotalFine.setText(""+totFin); 
         txtTotalCol.setText(""+totCol); 

        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        oldFin=0; 
       } 
      } 
      @Override 
      public void afterTextChanged(Editable arg0) 
      { 
       if(!(viewHolder.strFine.getText().toString().equals(""))) 
       { 
        try 
        { 

         Fine=viewHolder.strFine.getText().toString(); 
         newFin=Integer.parseInt(viewHolder.strFine.getText().toString()); 

         totFin=totFin+newFin; 
         totCol=totCol+newFin; 
         txtTotalFine.setText(""+totFin); 
         txtTotalCol.setText(""+totCol); 

        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        newFin=0; 
       } 

      } 

     }); 


     viewHolder.StatusCheckBox.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       int totalCurent,totalExtra,totalSaving,totalFine; 
       boolean checked = ((CheckBox) v).isChecked(); 
       Log.e("StatusCheckBox","StatusCheckBox onClickListener1"+checked); 
       if(checked==false) 
       { 
        Log.e("StatusCheckBox","StatusCheckBox onClickListener2"+checked); 
        try 
        { 
          if(!(viewHolder.strCurInt.getText().toString().equals(""))) 
          intCur=Integer.parseInt(viewHolder.strCurInt.getText().toString()); 
         else 
          intCur=0; 

         if(!(viewHolder.strExtraInt.getText().toString().equals(""))) 
          intExtra= Integer.valueOf(viewHolder.strExtraInt.getText().toString()); 
         else 
          intExtra=0; 

         if(!(viewHolder.strSaving.getText().toString().equals(""))) 
          intSaving=Integer.parseInt(viewHolder.strSaving.getText().toString()); 
         else 
          intSaving=0; 

         if(!(viewHolder.strFine.getText().toString().equals(""))) 
          intFine=Integer.parseInt(viewHolder.strFine.getText().toString()); 
         else 
          intFine=0; 

         if(!(txtTotalCur.getText().toString().equals(""))) 
          totalCurent=Integer.parseInt(txtTotalCur.getText().toString()); 
         else 
          totalCurent=0; 
         if(!(txtTotalExtra.getText().toString().equals(""))) 
          totalExtra=Integer.parseInt(txtTotalExtra.getText().toString()); 
         else 
          totalExtra=0; 
         if(!(txtTotalSaving.getText().toString().equals(""))) 
          totalSaving=Integer.parseInt(txtTotalSaving.getText().toString()); 
         else 
          totalSaving=0; 
         if(!(txtTotalFine.getText().toString().equals(""))) 
          totalFine=Integer.parseInt(txtTotalFine.getText().toString()); 
         else 
          totalFine=0; 


         totCurInt=totalCurent-intCur; 
         totExtra=totalExtra-intExtra; 
         totSav=totalSaving-intSaving; 
         totFin=totalFine-intFine; 
         totCol=totCurInt+totExtra+totSav+totFin; 

         txtTotalCur.setText(""+totCurInt); 
         txtTotalExtra.setText(""+totExtra); 
         txtTotalSaving.setText(""+totSav); 
         txtTotalFine.setText(""+totFin); 
         txtTotalCol.setText(""+totCol); 

         viewHolder.strCurInt.setText(""); 
         viewHolder.strExtraInt.setText(""); 
         viewHolder.strSaving.setText(""); 
         viewHolder.strFine.setText(""); 

         viewHolder.strCurInt.setEnabled(false); 
         viewHolder.strExtraInt.setEnabled(false); 
         viewHolder.strSaving.setEnabled(false); 
         viewHolder.strFine.setEnabled(false); 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        viewHolder.strCurInt.setEnabled(true); 
        viewHolder.strExtraInt.setEnabled(true); 
        viewHolder.strSaving.setEnabled(true); 
        viewHolder.strFine.setEnabled(true); 
       } 

      } 
     });  
    } 
    else 
    { 
     viewHolder = (ViewHolder) rowView.getTag(); 
    } 
    return rowView; 
} 

}

+0

你需要幫助,至少粘貼你的代碼,以便任何人都可以幫助你。 – strike

+0

'沒有代碼沒有幫助!!!!' –

+0

你應該使用ViewHolder來解決這種類型的問題。檢查這個http://stackoverflow.com/a/24865351/3912847 –

回答

0

與此一替換您getView。並讓我知道你是否面臨任何問題

@Override 
public View getView(int position, View rowView, ViewGroup parent) 
{ 
    final ViewHolder viewHolder; 
    if(rowView==null) 
    { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     viewHolder=new ViewHolder(); 
     rowView= inflater.inflate(R.layout.collectionrow, null, false); 

     viewHolder.strAccountNo=(TextView)rowView.findViewById(R.id.th_acntno); 
     viewHolder.strCustName = (TextView) rowView.findViewById(R.id.th_acntnm);  
     viewHolder.strCurInt = (TextView) rowView.findViewById(R.id.th_acntcur); 
     viewHolder.strExtraInt = (EditText) rowView.findViewById(R.id.th_acntextra); 
     viewHolder.strSaving = (EditText) rowView.findViewById(R.id.th_acntsaving); 
     viewHolder.strFine = (EditText) rowView.findViewById(R.id.th_fine); 
     viewHolder.strLoanBal = (TextView) rowView.findViewById(R.id.th_loanBal); 
     viewHolder.strSavingBal = (TextView) rowView.findViewById(R.id.th_saveBal); 
     viewHolder.StatusCheckBox=(CheckBox)rowView.findViewById(R.id.statusCheckBox); 
     rowView.setTag(viewHolder); 
    } 
    else 
    { 
     viewHolder = (ViewHolder) rowView.getTag(); 
    } 


viewHolder.strAccountNo.setText(statementBean_arr[position].getAccount_no()); 

     viewHolder.strCustName.setText(statementBean_arr[position].getCust_name()); 

     viewHolder.strCurInt.setText(""+statementBean_arr[position].getCur_int_amt()); 

     try 
     { 
      totCurInt= totCurInt+Integer.valueOf(viewHolder.strCurInt.getText().toString()); 

     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     totCol=totCurInt; 

     viewHolder.strExtraInt.setText(""+statementBean_arr[position].getExtra_int_amt()); 

     viewHolder.strSaving.setText(""+statementBean_arr[position].getSaving()); 

     viewHolder.strFine.setText(""+statementBean_arr[position].getFine()); 

     viewHolder.strLoanBal.setText(statementBean_arr[position].getLoan_bal()); 

     viewHolder.strSavingBal.setText(statementBean_arr[position].getSaving_bal()); 

     viewHolder.StatusCheckBox.setChecked(statementBean_arr[position].isCheckFlag()); 

     txtTotalCur.setText(""+totCurInt); 
     txtTotalCol.setText(""+totCol); 
     txtTotalExtra.setText(""+totExtra); 
     txtTotalFine.setText(""+totFin); 
     txtTotalSaving.setText(""+totSav); 


     viewHolder.strExtraInt.addTextChangedListener(new TextWatcher() 
     { 
      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
      {} 
      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) 
      { 
       if(!(viewHolder.strExtraInt.getText().toString().equals(""))) 
       { 
        try 
        { 
         ExtrInt=viewHolder.strExtraInt.getText().toString().trim(); 
         oldExtra= Integer.valueOf(viewHolder.strExtraInt.getText().toString()); 
         Log.e("CustomAdapterListForCollection","totCol 1 "+totCol); 
         totCol=totCol-oldExtra; 
         Log.e("CustomAdapterListForCollection","totCol 2 "+totCol); 
         totExtra=totExtra-oldExtra; 
         txtTotalExtra.setText(""+totExtra); 
         txtTotalCol.setText(""+totCol); 
         Log.e("CustomAdapterListForCollection","totCol 3 "+totCol); 
         Log.e("CustomAdapterListForCollection","oldExtra "+oldExtra); 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        oldExtra=0; 
       } 
      } 
      @Override 
      public void afterTextChanged(Editable arg0) 
      { 
       if(!(viewHolder.strExtraInt.getText().toString().equals(""))) 
       { 
        try 
        { 

         ExtrInt =viewHolder.strExtraInt.getText().toString(); 
         newExtra =Integer.parseInt(viewHolder.strExtraInt.getText().toString()); 
         totExtra=totExtra+newExtra; 
         totCol=totCol+newExtra; 
         txtTotalExtra.setText(""+totExtra); 
         txtTotalCol.setText(""+totCol); 

        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        newExtra=0; 
       } 

      } 
     }); 


     viewHolder.strSaving.addTextChangedListener(new TextWatcher() 
     { 
      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
      {} 
      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) 
      { 
       if(!(viewHolder.strSaving.getText().toString().equals(""))) 
       { 
        try 
        { 
         oldSav=Integer.parseInt(viewHolder.strSaving.getText().toString()); 
         totCol=totCol-oldSav; 
         totSav=totSav-oldSav; 
         txtTotalSaving.setText(""+totSav); 
         txtTotalCol.setText(""+totCol); 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        oldSav=0; 
       } 
      } 
      @Override 
      public void afterTextChanged(Editable arg0) 
      { 
       if(!(viewHolder.strSaving.getText().toString().equals(""))) 
       { 
        try 
        { 

         Saving=viewHolder.strSaving.getText().toString(); 
         newSav=Integer.parseInt(viewHolder.strSaving.getText().toString()); 

         totSav=totSav+newSav; 
         totCol=totCol+newSav; 
         txtTotalSaving.setText(""+totSav); 
         txtTotalCol.setText(""+totCol); 

        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        newSav=0; 
       } 

      } 
     }); 


     viewHolder.strFine.addTextChangedListener(new TextWatcher() 
     { 
      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
      {} 
      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
      { 

       if(!(viewHolder.strFine.getText().toString().equals(""))) 
       { 
        try 
        { 
         oldFin=Integer.parseInt(viewHolder.strFine.getText().toString()); 

         totCol=totCol-oldFin; 
         totFin=totFin-oldFin; 
         txtTotalFine.setText(""+totFin); 
         txtTotalCol.setText(""+totCol); 

        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        oldFin=0; 
       } 
      } 
      @Override 
      public void afterTextChanged(Editable arg0) 
      { 
       if(!(viewHolder.strFine.getText().toString().equals(""))) 
       { 
        try 
        { 

         Fine=viewHolder.strFine.getText().toString(); 
         newFin=Integer.parseInt(viewHolder.strFine.getText().toString()); 

         totFin=totFin+newFin; 
         totCol=totCol+newFin; 
         txtTotalFine.setText(""+totFin); 
         txtTotalCol.setText(""+totCol); 

        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        newFin=0; 
       } 

      } 

     }); 


     viewHolder.StatusCheckBox.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       int totalCurent,totalExtra,totalSaving,totalFine; 
       boolean checked = ((CheckBox) v).isChecked(); 
       Log.e("StatusCheckBox","StatusCheckBox onClickListener1"+checked); 
       if(checked==false) 
       { 
        Log.e("StatusCheckBox","StatusCheckBox onClickListener2"+checked); 
        try 
        { 
          if(!(viewHolder.strCurInt.getText().toString().equals(""))) 
          intCur=Integer.parseInt(viewHolder.strCurInt.getText().toString()); 
         else 
          intCur=0; 

         if(!(viewHolder.strExtraInt.getText().toString().equals(""))) 
          intExtra= Integer.valueOf(viewHolder.strExtraInt.getText().toString()); 
         else 
          intExtra=0; 

         if(!(viewHolder.strSaving.getText().toString().equals(""))) 
          intSaving=Integer.parseInt(viewHolder.strSaving.getText().toString()); 
         else 
          intSaving=0; 

         if(!(viewHolder.strFine.getText().toString().equals(""))) 
          intFine=Integer.parseInt(viewHolder.strFine.getText().toString()); 
         else 
          intFine=0; 

         if(!(txtTotalCur.getText().toString().equals(""))) 
          totalCurent=Integer.parseInt(txtTotalCur.getText().toString()); 
         else 
          totalCurent=0; 
         if(!(txtTotalExtra.getText().toString().equals(""))) 
          totalExtra=Integer.parseInt(txtTotalExtra.getText().toString()); 
         else 
          totalExtra=0; 
         if(!(txtTotalSaving.getText().toString().equals(""))) 
          totalSaving=Integer.parseInt(txtTotalSaving.getText().toString()); 
         else 
          totalSaving=0; 
         if(!(txtTotalFine.getText().toString().equals(""))) 
          totalFine=Integer.parseInt(txtTotalFine.getText().toString()); 
         else 
          totalFine=0; 


         totCurInt=totalCurent-intCur; 
         totExtra=totalExtra-intExtra; 
         totSav=totalSaving-intSaving; 
         totFin=totalFine-intFine; 
         totCol=totCurInt+totExtra+totSav+totFin; 

         txtTotalCur.setText(""+totCurInt); 
         txtTotalExtra.setText(""+totExtra); 
         txtTotalSaving.setText(""+totSav); 
         txtTotalFine.setText(""+totFin); 
         txtTotalCol.setText(""+totCol); 

         viewHolder.strCurInt.setText(""); 
         viewHolder.strExtraInt.setText(""); 
         viewHolder.strSaving.setText(""); 
         viewHolder.strFine.setText(""); 

         viewHolder.strCurInt.setEnabled(false); 
         viewHolder.strExtraInt.setEnabled(false); 
         viewHolder.strSaving.setEnabled(false); 
         viewHolder.strFine.setEnabled(false); 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        viewHolder.strCurInt.setEnabled(true); 
        viewHolder.strExtraInt.setEnabled(true); 
        viewHolder.strSaving.setEnabled(true); 
        viewHolder.strFine.setEnabled(true); 
       } 

      } 
     }); 



    return rowView; 
+0

ji ..我也這樣做了,但也有同樣的問題.....第一種情況下,我的記錄得到重複加總和的變化,因爲我滾動我的列表和第二種情況的總和問題仍然存在 –

+0

抱歉,但不能理解你想說什麼? –

+0

在getview中聲明tolcol –

相關問題