扩展其他子元素时,可扩展列表中的单选按钮仍然存在

时间:2013-03-18 00:51:53

标签: java android radio-button radio-group expandablelistadapter

我想在我的Android应用中制作一个可扩展的广播组列表。列表的每个元素应包含(打开时)几个单独的无线电组。到目前为止,我每个孩子只测试一个放射性组,我已经遇到了一个我自己似乎无法解决的问题。

当我展开可扩展列表的父级时,它似乎正常工作。放射线组的放射线按钮(现在是父母的唯一子元素)以它们应该的方式显示。但是,当我展开可扩展列表的另一个父级时,单选按钮将添加到另一个子组的现有按钮中。这意味着:一旦我扩展了几个子组,每个扩展的子组都包含大量的单选按钮,充斥整个屏幕。 简单地说,当我扩展父1时,我得到了选择

  • 没有
  • 也许

当我扩展第二个父母时,我得到了无线电选择

  • 没有
  • 也许
  • 没有
  • 也许
  • 可能

...它们也被添加到父级1内的单选按钮组中。所以现在我有2个大的扩展列表元素。当我展开另一个组时,选择也会添加到所有子视图中,依此类推,等等......

因此,遵循简单的逻辑,似乎所有单选按钮都没有放入新组(每次扩展子列表时新创建的组),而是将它们添加到同一个已存在的组中。

我已经能够将问题几乎与我的ExpandableListAdapter类中的getChildView函数隔离开来。这是导致上述输出的代码:

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
        ViewGroup parent) {
    ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition);
    if (view == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(R.layout.child_layout, null);
    }

    ArrayList<HashMap<String,String>> inputDetailsList = child.getInputfields();
    //RadioGroup rg = new RadioGroup(view.getContext());
    RadioGroup rg = (RadioGroup) view.findViewById(R.id.radiogroup);
    int has_radiogroup = 0;

    for (int i = 0; i <inputDetailsList.size(); i++){
        if (inputDetailsList.get(i).get("type").equals("radio")){
            has_radiogroup = 1;
            RadioButton rb = new RadioButton(rg.getContext());
            rb.setText(inputDetailsList.get(i).get("textcontent"));
            rg.addView(rb);
        }
    }

    return view;
}

以下是用作子布局的xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:background="@color/ExpandChildBackground"
android:orientation="vertical" >

<TextView
    android:id="@+id/tvChild"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/Black"
    android:textSize="17dip" />

<RadioGroup
    android:id="@+id/radiogroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
</RadioGroup>
</LinearLayout>

我怀疑我的错误可能是这一行:

RadioGroup rg = (RadioGroup) view.findViewById(R.id.radiogroup);

它查找我在膨胀的子布局(R.layout.child_layout)中创建的无线电组。但是它不会为每个展开的子视图使用新的视图,而是使用现有的视图来应用按钮。换句话说,似乎无线电组从未得到其“完成”和/或子视图永远不会得到必须使用新无线电组而不是旧无线电组的标志。

我尝试过使用以下行为每个子视图创建一个新的RadioGroup(您可以看到它是上面代码中的注释行),而不是使用我在布局中定义的无线电组。 制作一个新的无线电组看起来像这样:

RadioGroup rg = new RadioGroup(view.getContext());

然而,这不起作用,它不显示任何无线电组,更不用说按钮了。我也尝试从布局xml文件中删除radiogroup元素(因为它还没有使用)。仍然没有运气。对每个元素使用简单的无组织文本视图似乎工作正常,因此这也指出了我的无线电组实现的问题。

老实说,我现在没有想法。我可能会得到错误的上下文,但我不知道要使用哪种上下文。如果有人能够指出我正确的方向,我将非常感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

如果有人有兴趣,我已经能够自己解决问题了。我只是删除了if条件[if (view == null)],因此新代码如下所示:

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
        ViewGroup parent) {

    ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition);
    LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = infalInflater.inflate(R.layout.child_layout, null);

    ArrayList<HashMap<String,String>> inputDetailsList = child.getInputfields();

    RadioGroup rg = (RadioGroup) view.findViewById(R.id.radiogroup);
    int has_radiogroup = 0;

    for (int i = 0; i <inputDetailsList.size(); i++){

        if (inputDetailsList.get(i).get("type").equals("radio")){
            System.out.println("type: "+inputDetailsList.get(i).get("type"));
            has_radiogroup = 1;
            RadioButton rb = new RadioButton(rg.getContext());
            rb.setText(inputDetailsList.get(i).get("textcontent"));
            System.out.println("textcontent: "+inputDetailsList.get(i).get("textcontent"));
            rb.setId(Integer.parseInt(inputDetailsList.get(i).get("value")));

            if (has_radiogroup == 1){
                rg.addView(rb);
            }
        }
    }

    return view;
}

现在有道理。每次生成新的子视图时都会调用LayoutInflater,即扩展组。在此之前,它只使用了填充了radiobuttons的现有视图(视图不为空),并将新的radiobuttons添加到现有视图中,导致我的列表充斥着屏幕。

感谢任何人试图解决问题。

答案 1 :(得分:0)

这对我有用。 对于 RadioButtons和CheckBoxs

        android:focusable="false"