在列表视图中使用时,Edittext在滚动时会丢失数据

时间:2014-07-15 10:18:38

标签: android-layout android-listview android-edittext

我知道这个问题在stackoverflow中重复了很多次,仍然找不到合适的解决方案。我正在构建一个Android应用程序,我在Listview中使用Edit文本。当我在编辑文本中输入一些数据并滚动到下面的部分并返回时,输入的数据将丢失,或者它将位于列表视图的不同行视图中。

我按照下面的堆栈溢出帖子编写了代码。仍然没有用。

Edit Text in custom List View Loses Value on Scroll

以下是我的适配器代码。

package com.example.listview;

import java.util.List;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;



class MynewAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
int temp;
public String[] text;
//private int editingPosition = 0;

MynewAdapter(Activity context, List<Model> list) {
        super(context, R.layout.row, list);
        this.context = context;
        this.list = list;
        text= new String[list.size()];
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("getview:"+position+" "+convertView);
        ViewHolder holder = null;
        temp=position;
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {

            holder = new ViewHolder();
            convertView = mInflater
                    .inflate(R.layout.row, parent, false);

            holder.text = (TextView) convertView
                    .findViewById(R.id.label);

            holder.address = (EditText) convertView
                    .findViewById(R.id.txtAddress);
            holder.address.setTag(position);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //holder.address.removeTextChangedListener(watcher);

        holder.address.setText(text[temp]);
       /*
        holder.address.setOnFocusChangeListener(new OnFocusChangeListener(){       
            public void onFocusChange(View v, boolean hasFocus) {
              if(hasFocus) editingPosition = temp;
            }
        });
        */   
      holder.address.addTextChangedListener(new TextWatcher() {

          public void onTextChanged(CharSequence s, int start, int before, int count) {
              System.out.println("onTextChanged size= "+list.size()+" editingposition = "+temp+" string = "+s.toString());
              text[temp] = s.toString();
          }
          public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
          public void afterTextChanged(Editable s) { }
      });
      holder.text.setText(list.get(position).getName());

        return convertView;
    }

    class ViewHolder {
        protected TextView text;
        protected EditText address;
    }
}

我已经在get view方法和文本观察器中添加了trace。当应用程序打开时,将为所有可见列表项调用视图,并且convertview将为null,因为无需回收。当我在Listview项目1的编辑字段中输入文本时,仍然位置参数将是6.这就是我在代码中找到的错误。那么无论如何都要找到文本被编辑的项目的位置?这将真正解决问题。

07-16 10:58:48.449: I/System.out(825): getview:0 null
07-16 10:58:48.469: I/System.out(825): getview:1 null
07-16 10:58:48.469: I/System.out(825): getview:2 null
07-16 10:58:48.479: I/System.out(825): getview:3 null
07-16 10:58:48.479: I/System.out(825): getview:4 null
07-16 10:58:48.489: I/System.out(825): getview:5 null
07-16 10:58:48.489: I/System.out(825): getview:6 null
07-16 10:59:33.309: I/System.out(825): onTextChanged size= 18 editingposition = 6 string = p
07-16 10:59:54.659: I/System.out(825): onTextChanged size= 18 editingposition = 6 string = pr
07-16 11:00:24.599: I/System.out(825): onTextChanged size= 18 editingposition = 6 string = s
07-16 11:00:24.949: I/System.out(825): onTextChanged size= 18 editingposition = 6 string = su

1 个答案:

答案 0 :(得分:0)

  1. 您需要将新编辑的文本保存在用于设置文本的数据源中。在您的情况下public String[] text并致电notifyDataSetChanged()。如果您没有保存更新的数据,那么适配器将只获取之前的值并将其显示在列表视图中。

  2. 为每个textview使用新的textWatcher。

  3. 我建议你使用它:

      holder.address.addTextChangedListener(new TextWatcher() {
    
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  System.out.println("onTextChanged size= "+list.size()+" editingposition = "+editingPosition+" string = "+s.toString());
                  text[position] = s.toString();
              }
              public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
              public void afterTextChanged(Editable s) { }
          });