将ListView EditText值存储或维护到ArrayList <string> </string>中

时间:2013-02-11 11:51:20

标签: android listview android-arrayadapter android-edittext

我的问题是: - 我想将ListView的所有EditText值存储到ArrayList
在我的应用中,我Custom ListView行为EditText。只需考虑listview有20行。当我执行此任务时,我首先面临滚动问题ListView(这意味着EditText的值被搞砸了(放在其他地方)),但幸运地解决了它 现在我想将所有值存储到ArrayList<String>
是的我也将代码编写到我的适配器中。但是当将该列表设置为外部使用时,获取异常NullPointer 。另外我也不想在getView中设置ArrayList(在查看回收时会导致问题)我知道TextWatcher Listener的异常becoz。那么我应该选择什么样的替代方案。我也没有任何按钮来保存数据。只需要存储值,因为值会逐一显示在ListView的EditText中。
这是我的适配器类: -

public class MyAdapter extends ArrayAdapter<String> 
{
    private LayoutInflater mInflater;       
     public ArrayList<String> _values;
     ArrayList<String> data;
     Model model;
    public MyAdapter(Context context,int res,ArrayList<String> obj) {
        super(context,res,obj);
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);             
        _values=new ArrayList<String>();
        model=new Model();
        this.data=obj;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview, null);
            holder = new ViewHolder();               
            holder.caption = (EditText) convertView.findViewById(R.id.editText1);              
           holder.tv=(TextView)convertView.findViewById(R.id.textView1);
           holder.tv.setText(data.get(position));
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.tv.setText(data.get(position));
        }

        holder.caption.addTextChangedListener(new TextWatcher()
        {
            public void afterTextChanged(Editable edt) 
            {
                _values.add(edt.toString());
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
        });

        convertView.setTag(holder);
        holder.ref=position;
        holder.caption.setText(_values.get(position));
        model.setData(_values);//here getting exception
        return convertView;
    }
}

class ViewHolder {
    EditText caption;   
    TextView tv;
    int ref;
}

这是模型类I用于外部目的关于该字符串

 public class Model 
 {
   ArrayList<String> data;

   public ArrayList<String> getData() {
       return data;
   }
   public void setData(ArrayList<String> data) {
    this.data = data;
    for(String str : data)
        Log.v("data=", str);
   }
}

1 个答案:

答案 0 :(得分:0)

我不确定但是这可以帮到你

Arrays.sort(data);

而不是

MyAdapter ma = new MyAdapter(<args>) 
ma.notifydatasetchanged();
<ListViewInstance>.setAdapter(ma);