Iterate through ListView and get EditText-Field va

2019-01-11 12:52发布

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.

4条回答
Fickle 薄情
2楼-- · 2019-01-11 13:22

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()));
        }
    }
    ....
}
查看更多
Emotional °昔
3楼-- · 2019-01-11 13:42

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());
    }
....
}
查看更多
在下西门庆
4楼-- · 2019-01-11 13:43

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());
    }
....
}
查看更多
Ridiculous、
5楼-- · 2019-01-11 13:46

Regarding why the selected answer might sometimes throw a null on the "et = (EditText) v.findViewById(i);"

Would it not be at that point trying to select the item at position i WITHIN the view that you had previously selected? Thus if there are fewer items in the child views than there are items in the whole list, you would get null due to i trying to select items that don't exist.

Perhaps a nested loop could solve this problem? Or if you already know the index of the element you need, simply give it the index directly.

查看更多
登录 后发表回答