Iterate through ListView and get EditText-Field values

46,390

Solution 1

Solved the Problem with the help of a friend:

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.getAdapter().getView(i, null, null);
        et = (EditText) v.findViewById(i);
        mannschaftsnamen.add(et.getText().toString());
    }
....
}

Solution 2

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());
    }
....
}

Solution 3

I think the answer from @Arius was quite close to be right solution. I just want to highlight that View you should get is View which contain with ListView. So you need to getChildAt(position) related with your ListView.

private void ButtonClick() {
/** get all values of the EditText-Fields
find your ListView local variable you had created for example :
private ListView listview; 
You also need to catch null value of EditText first. */
ArrayList<String> mannschaftsnamen = new ArrayList<String>();
EditText et;
    for (int i = 0; i < listview.getCount(); i++) {
        et = (EditText) listview.getChildAt(i).findViewById(R.id.editText1);
        if (et!=null) {
           mannschaftsnamen.add(String.valueOf(et.getText()));

           /** you can try to log your values EditText */
           log.v("ypgs", String.valueOf(et.getText()));
        }
    }
    ....
}
Share:
46,390

Related videos on Youtube

Andreas Schneider
Author by

Andreas Schneider

Updated on June 16, 2020

Comments

  • Andreas Schneider
    Andreas Schneider almost 4 years

    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.

  • tejas
    tejas almost 11 years
    I am trying this code but the et or relevant field always returns null for me. Do you know why?
  • tejas
    tejas almost 11 years
    This line et = (EditText) v.findViewById(i); throws me null, can you guess why?
  • lolliloop
    lolliloop almost 11 years
    do you know why et = (EditText) v.findViewById(i); throws null?
  • manWe
    manWe almost 11 years
    Maybe because you need to supplement it with the real name of that element in XML?
  • Diffy
    Diffy over 9 years
    But adapter size can be different from the listview size.
  • espinchi
    espinchi over 9 years
    myList.getCount() or myList.getChildCount()? The latter looks like more consistent
  • TechSpellBound
    TechSpellBound almost 9 years
    findViewById() is supposed to take resource ID and not the index!
  • harikrishnan
    harikrishnan about 8 years
    here, above code is working fine. after entered editext values, am trying to get, that time getting empty values only from EditText..