0

在视图中显示时,如果我滚动超过第一个框,我输入的值会EditText同时显示在第一个和最后一个框中。将值保存到数据库时,它只是按预期只保存最后一个值。EditTextEditTextEditText

是什么导致了这种行为?这是我的完整代码:

package com.example.expensemanager1;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;



public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
    private final List<Model> list;
    private final Activity context;
    final int i;
    LayoutInflater mInflater;
    public InteractiveArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.spared_list_row, list);
        this.context = context;
        this.list = list;
        i=list.size();
        notifyDataSetChanged();
    }




    static class ViewHolder {
        protected TextView membname;
        protected EditText membamount;
    }



    public class MyTextWatcher implements TextWatcher
    {
        private EditText editText;
        Model elemente;
        public MyTextWatcher(EditText editText, Model m)
        {
            this.editText = editText;
            this.elemente = m;
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

            Log.d("TEST", "In afterTextChanged"); 

            editText.setFocusable(true);

            String ssss= s.toString();

            if(editText.getText().toString().length()>0)
             list.get((Integer)editText.getTag()).setamount(Double.parseDouble(s.toString()));
            else
                list.get((Integer)editText.getTag()).setamount(0);



            double sum=0.0;
            for (int j=0; j<i ; j++)
            {
                Model m1= list.get(j);                      

                sum += m1.getamount();          

            }

            Log.d("TEST", "sum" +sum );

            double balance = New_spared_list.getTextView();
            Log.d("TEST", "balance = "+ balance);
            sum = balance - sum; 
            New_spared_list.setX(sum);

            Log.d("TEST", "sum" +sum );



        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

        }
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         View view = null;
        ViewHolder viewHolder = new ViewHolder();


        if (convertView == null) {
            final Model elemente = list.get(position);
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.spared_list_row, null);


            viewHolder.membname = (TextView) convertView.findViewById(R.id.membersTextView2);
            viewHolder.membamount = (EditText) convertView.findViewById(R.id.amount);
            final ViewHolder holder = viewHolder;
            viewHolder.membamount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //do something
                    LinearLayout parent = (LinearLayout) v.getParent();            
                    EditText membamount = (EditText) parent.findViewById(R.id.amount); 

                    membamount.setFocusable(true);
                membamount.setFocusableInTouchMode(true);
                membamount.requestFocus();

                }

            });




            viewHolder.membamount.addTextChangedListener(new MyTextWatcher(viewHolder.membamount, (Model) viewHolder.membamount.getTag()));



            convertView.setTag(viewHolder);
            convertView.setTag(R.id.membersTextView2, viewHolder.membname);
            convertView.setTag(R.id.amount, viewHolder.membamount);
           // viewHolder.membamount.setTag(list.get(position));
           // viewHolder.membamount.setHint(String.valueOf((list.get(position).getamount())));

        } else {

            viewHolder = (ViewHolder) convertView.getTag();
            ((ViewHolder) convertView.getTag()).membamount.setTag(list.get(position));
        }

        viewHolder.membamount.setTag(position);
        if(position % 2 == 0){  
            convertView.setBackgroundColor(Color.rgb(231, 247, 253));
           }

      //  viewHolder.membamount.setTag(list.get(position));


        viewHolder.membamount.setHint(String.valueOf(list.get(position).getamount()));
        viewHolder.membname.setText(list.get(position).getName());


        return convertView;
    }

}
4

1 回答 1

0

在 getView() 结束时,还应该更改 membamount 中的文本;否则,如果它重用一个转换视图,它会保留以前的文本。

viewHolder.membamount.setText(list.get(position).getAmount());
于 2013-04-18T14:35:34.887 回答