遍历 ListView 并获取 EditText-Field 值 [英] Iterate through ListView and get EditText-Field values

查看:30
本文介绍了遍历 ListView 并获取 EditText-Field 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 ListView (myList),每行有一个 EditText-Field (R.id.mannschaften).在 ListView 下,我创建了一个 Button 并在 OnCreate()-Method 中为它设置了一个 OnClickListener.现在,当单击 Button 时,我想遍历 ListView 并获取每一行中 EditText-Field 的值并将它们保存在 String-List 中.但我该怎么做?

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?

这是我的代码:

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

我已经知道,getChildAt 仅适用于可见行,但我不知道如何以不同的方式执行此操作.

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

推荐答案

在朋友的帮助下解决了问题:

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

这篇关于遍历 ListView 并获取 EditText-Field 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆