user6704378
user6704378

Reputation: 63

add all edittexts data of listview in arraylist

I have a list view containing textview and edit texts and i have to add these values in array list.As i am trying it not all values are added to Arraylist,only values of that particular visible view gets added.How should I modify my code so that all the data gets added to 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);
            }
        }
    }
}

Upvotes: 0

Views: 125

Answers (1)

SANAT
SANAT

Reputation: 9257

It happens because android recycle the view of ListView and Recyclerview. You need to add(save) data of EditText by implementing OnTextChangeListener. By implementing it you can get the entered text and assign it to particular position in List<>.

Example :

@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;
    }

Upvotes: 1

Related Questions