在arraylist中添加listview的所有edittexts数据

时间:2016-10-12 11:38:59

标签: android

我有一个包含textview和编辑文本的列表视图,我必须在数组列表中添加这些值。我正在尝试它并不是所有的值都添加到Arraylist中,只添加了特定可见视图的值。我该怎么办?修改我的代码,以便将所有数据添加到arraylist。

list = (ListView) findViewById(R.id.list);
personList = new ArrayList<HashMap<String,String>>();
flags=0;
getData();
placeord=(Button)findViewById(R.id.placeorder);

placeord.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(isNetworkConnected()) {
            ArrayList<String> m = new ArrayList<String>();
            ArrayList<String> si = new ArrayList<String>();
            EditText q;
            TextView sizes;

            for (int i =0; i<= list.getLastVisiblePosition() - list.getFirstVisiblePosition(); i++) {
                View  v = list.getChildAt(i);
                q = (EditText) v.findViewById(R.id.numberofitems);
                sizes = (TextView) v.findViewById(R.id.size);

                if(q.getText().toString().length()>0) {

                    si.add(sizes.getText().toString());
                    flags=1;

                }
                m.add(q.getText().toString());
            }

            if (flags == 0) {
                Toast.makeText(getApplicationContext(), "please add some quantity", Toast.LENGTH_SHORT).show();
            } else {
                //Toast.makeText(getApplicationContext(),val,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(placecolor.this, placeorder2.class);
                Bundle extras1 = new Bundle();
                extras1.putStringArrayList("numberofitems", m);
                extras1.putStringArrayList("sizes", si);
                intent.putExtras(extras1);
                startActivity(intent);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是因为android会回收 ListView Recyclerview 的视图。您需要通过实施 OnTextChangeListener 来添加(保存) EditText 的数据。通过实施它,您可以获取输入的文本并将其分配到List&lt;&gt;。

中的特定位置

示例:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {

            holder = new ViewHolder();
            LayoutInflater inflater = context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.listview_list, null);
            holder.textView = (TextView) convertView.findViewById(R.id.textView);
            holder.editText = (EditText) convertView.findViewById(R.id.editText);    

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        holder.ref = position;

        holder.textView.setText(arr[position].name);
        holder.editText.setText(arr[position].nameValue);
        holder.editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                arr[holder.ref].nameValue = arg0.toString(); // Set value in list
            }
        });

        return convertView;
    }