Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through ListView and get EditText-Field values

I've created a ListView (myList) with one EditText-Field in each row (R.id.mannschaften). Under the ListView, I've created a Button and set an OnClickListener for it in the OnCreate()-Method. Now when the Button is clicked, I want to iterate through the ListView and get the value of the EditText-Field in every row and save them in a String-List. But how do I do that?

Here is my Code:

private void ButtonClick() {     /** get all values of the EditText-Fields */     View v;     ArrayList<String> mannschaftsnamen = new ArrayList<String>();     EditText et;      for (int i = 0; i < myList.getCount(); i++) {         v = myList.getChildAt(i);         et = (EditText) v.findViewById(R.id.mannschaften);         mannschaftsnamen.add(et.getText().toString());     }     ..... } 

I already know, that getChildAt is only for the visible rows, but I don't know how to do it differently.

like image 355
Andreas Schneider Avatar asked Jan 12 '13 13:01

Andreas Schneider


1 Answers

When you use myList.getAdapter().getView(i,null,null) you are getting a new instance of the item view, try the ListView.getChildAt(position) method like this:

private void ButtonClick() {     /** get all values of the EditText-Fields */     View v;     ArrayList<String> mannschaftsnamen = new ArrayList<String>();     EditText et;     for (int i = 0; i < myList.getCount(); i++) {         v = myList.getChildAt(i);         et = (EditText) v.findViewById(R.id.editText1);         mannschaftsnamen.add(et.getText().toString());     } .... } 
like image 126
Mohamed F Avatar answered Sep 21 '22 18:09

Mohamed F