自定义ListView与编辑文本

时间:2015-05-29 05:31:31

标签: android android-listview

您好我有一个带有四个编辑文本字段的自定义列表视图。最初这些编辑文本由来自sqlite的数据填充。这些编辑文本包含整数值。有一个Textfield,显示编辑文本中所有这些值的总和。每次我滚动列表时总和都会改变。

公共类CustomAdapterListForCollection扩展了ArrayAdapter {

    for i in reader:
       try: 
           print i
       except UnicodeEncodeError:
           pass

}

1 个答案:

答案 0 :(得分: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;