动态列表视图,包含android中的动态值

时间:2012-06-28 17:09:12

标签: android android-listview

您好我是android新手,我正在开发一个应用程序,其中数据显示在listview中。 Listview包含一个textview和微调器。但是对于特定条件,例如,如果有一个Qualification字段具有一个或多个用户想要选择的选项。所以在那个特定条件下我必须给出多个复选框。数据通过webservices传递。我将数据存储在POJO中。我已经开发了textview和spinner使用inflater。但我面临着复选框控制的问题。当特定情况发生时,我如何动态复选框。 Problem

编辑我为此创建两个单独的XML文件。我对该复选框XML文件使用以下代码。

public View getView(final int position, View convertView, ViewGroup parent) {
    for (int k = 0; k < values.length; k++) {   
        convertView = inflater.inflate(R.layout.filter_brands, null);  
    }
}

但XML文件只能调用。而值包含7。

2 个答案:

答案 0 :(得分:0)

我假设你已经在使用某种类型的自定义适配器,并在该适配器的getView()中膨胀视图。现在您只需要根据需要创建另一个带有复选框的xml文件。根据您的条件,在需要时为相应的xml充气并启动复选框的文本。

例如,假设您有一个像这样的xml,可以说它叫做radio_buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <RadioButton
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

    </RadioGroup>

</LinearLayout>

然后在你的代码中使用你的条件来膨胀这个xml而不是默认的xml。

您可以通过执行view.findViewById(R.id.button1)从单选按钮中获取文本并设置文本。

答案 1 :(得分:0)

我建议您按照迈克回答的评论中提出的想法。

但如果你真的想以编程方式创建一些东西,可以这样做。

CheckBox check1 = new CheckBox(YourClassName.this);
CheckBox check2 = new CheckBox(YourClassName.this);

...
 LinearLayout ll = new LinearLayout(this);
 ll.setOrientation(1);

        ll.addView(check1,0);
        ll.addView(check2,1);