如何在滚动时避免在Listview中刷新edittext值?

时间:2014-06-11 04:58:43

标签: android listview android-listview listviewitem

我有一个包含2个textviews和一个编辑文本的列表视图。我已经使用自定义适配器来填充列表视图(因为我使用相同的方法避免一个月前刷新复选框),我的问题是如何在列表视图滚动时避免编辑文本的引用,因为我想保存每个edittext上的数据。 textwatcher会以任何方式帮助避免刷新和保存吗?如果是,请帮助。我已在此处包含我的模型和自定义适配器类:

模特课:

public class InternalListModel {
    String no;
    String name;
    String value;
    public InternalListModel(String internNo, String internName) {
        // TODO Auto-generated constructor stub
        this.no=internNo;
        this.name=internName;
        value="";
    }
    public String getNo(){
        return no;
    }
    public String getName(){
        return name;
    }
    public String getValue(){
        return value;
    }
    public void setValue(String value){
        this.value=value;
    }
}

自定义适配器类:

public class InternCustomAdapter extends ArrayAdapter<InternalListModel>
{
    private final List<InternalListModel> internnoname;
    private final Internal6 context;

    public InternCustomAdapter(Internal context,
            List<InternalListModel> internnoname) {
        super(context,R.layout.internlistdecor,internnoname);
        // TODO Auto-generated constructor stub
        this.context=context;
        this.internnoname=internnoname;
    }
    static class ViewHolder{
        protected TextView textview,textview1;
        protected EditText edittext;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder viewHolder;
        if(convertView ==null){
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.internlistdecor,null);
            viewHolder = new ViewHolder();
            viewHolder.textview=(TextView)convertView.findViewById(R.id.tvintNO);
            viewHolder.textview1=(TextView)convertView.findViewById(R.id.tvintName);
            viewHolder.edittext = (EditText)convertView.findViewById(R.id.etintValue);
            viewHolder.edittext.addTextChangedListener(new TextWatcher() {

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

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

                }
                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub  
                }
            });
            convertView.setTag(viewHolder);
            convertView.setTag(R.id.tvintNO, viewHolder.textview);
            convertView.setTag(R.id.tvintName, viewHolder.textview1);
            convertView.setTag(R.id.etintValue, viewHolder.edittext);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textview.setText(internnoname.get(position).getNo());
        viewHolder.textview1.setText(internnoname.get(position).getName());
        viewHolder.edittext.setText(internnoname.get(position).getValue());
        return convertView;
    }

}

1 个答案:

答案 0 :(得分:1)

您可以在位置(索引)的字段中设置输入的文字

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    internnoname.get(position).setValue(s.toString());
}

即使视图被回收,取决于位置,也会正确设置为EditTexView,因为以下代码:

viewHolder.edittext.setText(internnoname.get(position).getValue());