【问题标题】:Iterate through ListView and get EditText-Field values遍历 ListView 并获取 EditText-Field 值
【发布时间】:2012-12-26 22:54:42
【问题描述】:

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

这是我的代码:

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 仅适用于可见行,但我不知道如何以不同的方式进行操作。

【问题讨论】:

    标签: android listview android-edittext


    【解决方案1】:

    当您使用myList.getAdapter().getView(i,null,null) 时,您将获得项目视图的新实例,请尝试使用ListView.getChildAt(position) 方法,如下所示:

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

    【讨论】:

    • 但适配器大小可以与列表视图大小不同。
    • myList.getCount()myList.getChildCount()?后者看起来更一致
    【解决方案2】:

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

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

    【讨论】:

    • 我正在尝试此代码,但 et 或相关字段总是为我返回 null。你知道为什么吗?
    • 这一行 et = (EditText) v.findViewById(i);将我抛出 null,你能猜到为什么吗?
    • 你知道为什么 et = (EditText) v.findViewById(i);抛出空值?
    • 可能是因为你需要用XML中那个元素的真实名称来补充它?
    • findViewById() 应该采用资源 ID 而不是索引!
    【解决方案3】:

    我认为@Arius 的答案非常接近正确的解决方案。我只想强调你应该得到的ViewView,其中包含ListView。所以你需要getChildAt(position)与你的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()));
            }
        }
        ....
    }
    

    【讨论】:

      【解决方案4】:

      关于为什么选择的答案有时可能会在“et = (EditText) v.findViewById(i);”上抛出空值

      难道不是在那个时候尝试在您之前选择的视图中选择位置 i 处的项目吗?因此,如果子视图中的项目少于整个列表中的项目,由于我试图选择不存在的项目,您将得到 null。

      也许嵌套循环可以解决这个问题?或者如果你已经知道你需要的元素的索引,直接给它索引。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-31
        • 2015-09-21
        • 2017-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-22
        • 2021-08-04
        相关资源
        最近更新 更多