【问题标题】:ExpandableListView with 2 items every row, get child valuesExpandableListView 每行有 2 个项目,获取子值
【发布时间】:2013-03-28 11:08:24
【问题描述】:

我将ExpandableListActivityExpandListAdapter 结合使用。

我的布局:

---------------------------------
|  <TextView>                   |
|                               |
|-------------------------------|
| <ExpandableList>            | |
|  group1                     | |
|   child1: textView editText | |
|   child2: textView switch   | |
|  group2                     | |
|   child1: textView textView | |
|-------------------------------| 
| <footerView>                  |
|  [button]                     |
---------------------------------   

这是我的 child_layout(删除了不必要的部分):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvChild"
        android:layout_width="400px"
        android:layout_height="wrap_content"
        android:focusable="false" />

    <EditText
        android:id="@+id/etChild"
        android:layout_width="400px"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <TextView
        android:id="@+id/tvChild2"
        android:layout_width="600px"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <Switch
        android:id="@+id/switchChild"
        android:layout_width="400px"
        android:layout_height="wrap_content"
        android:textOff="NO"
        android:textOn="YES"
        android:visibility="gone" />
</LinearLayout>

这是我为每个孩子添加按钮、开关或 textView 的方式:

    Switch swc = (Switch) view.findViewById(R.id.switchChild);
    EditText etc = (EditText) view.findViewById(R.id.etChild);
    TextView tvc = (TextView) view.findViewById(R.id.tvChild2);

    if (groupPosition == 0) {
        swc.setVisibility(View.GONE);
        tvc.setVisibility(View.GONE);
        etc.setVisibility(View.GONE);
        if (childPosition == 0) 
            etc.setVisibility(View.VISIBLE);
        if (childPosition == 1) 
            etc.setVisibility(View.VISIBLE);
        if (childPosition == 2) {
            tvc.setVisibility(View.VISIBLE);
            tvc.setText(GlobalVariables.currentSelectedCar);
        }
    }

因此,每个孩子都有一个不会更改的TextView 加上一个用户可编辑的EditText/TextView/Switch。当我点击底部的按钮时,我想获取每个孩子的可编辑项目值。

【问题讨论】:

  • 你成就了这件事吗?我正在寻找相同但从子视图中检索数据的问题
  • 如果仍有问题请联系我

标签: android expandablelistview children expandablelistadapter


【解决方案1】:

我相信您可以采用与获取常规 ListView 的一项中包含的信息类似的方式来处理此问题。 ExpandableListView 具有内置方法,可让您找出单击了哪个项目。一旦您知道单击了列表中的哪个项目,您就可以简单地从与该项目关联的各个视图中提取值。我会做以下事情:

(1) setOnGroupClickListener 为您的ExpandableListViewonGroupClick 侦听器获取被选中的视图及其在列表中的位置等信息。

(2) 一旦您知道选择了列表中的哪个项目,您应该能够以两种方式之一提取数据。如果您要查找的数据是固定的,请使用位置指示器从支持列表的适配器检索该数据。如果您正在该选定项目中寻找视图(听起来像您),请在返回的视图上使用 findViewById。例如 - onGroupClick 方法提供 View v 作为参数

String input = v.findViewById(R.id.etChild).getText().toString();

onGroupClick 提供的视图上调用findViewById 应该允许您在该视图中查看您提供的id 的子视图。这样您就可以从每个列表项中的输入字段中提取数据。

【讨论】:

  • 感谢您的回复。我花了很多时间在ExpandableListActivityExpandListAdapter上,看来你的建议是正确的。
【解决方案2】:

#Rarw 的创意

TextView input = (TextView) v.findViewById(R.id.child_item_elv);
Toast.makeText(getActivity().getApplicationContext(), "child clicked : " + input.getText() , Toast.LENGTH_SHORT).show();

这个工作,在OnGroupListener内部

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多