0

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);
            }
        }
    }
}
5
  • Total list view count is int count=lstView.getAdapter().getCount(); Commented Oct 12, 2016 at 11:44
  • how would getting count help... Commented Oct 12, 2016 at 11:46
  • for (int i =0; i <= lstView.getAdapter().getCount(); i++) I think that's what he meant? Commented Oct 12, 2016 at 11:50
  • i added this before but it get unfortunately stop because I guess list.getchildat was giving null pointer exception,so I changed the code Commented Oct 12, 2016 at 11:53
  • Yeah because getChildAt only gives the "view" childs not the actual ones. I suggest @SANAT answer! Commented Oct 12, 2016 at 11:56

1 Answer 1

1

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;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

can you please explain with an example.
@user6704378 check the example.
@user6704378 is it helpful?
@user6704378 Accept this answer, if it helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.