0

我有一个包含 10 个多项选择题的列表,每个问题都有一个“RadioGroup”。但是,当我为一个问题选择一个选项时,其他问题的一些选项会自动选择。

假设我为问题选择了一个选项 - 1。然后问题编号 - 5、9 会自动选择相同的选项。

当我为问题 - 2 选择一个选项时,问题编号 - 6、10 会自动选择相同的选项。

这不应该发生。当我选择一个选项时,应该只检查该 RadioGroup 中的那个选项。请帮我解决这个问题。

我正在开发基于移动设备的测试。当用户输入他的用户名和密码时,服务器将对其进行身份验证。对于授权用户,它将以 JSONObject 形式返回试卷。每个考试将有一个exam_id 和一个包含10 个多项选择题的列表,而每个问题都有一个唯一的“id”、“question”和四个选项“A”、“B”、“C”和“D”,如下所示。“问题”对象是一个 JSONArray。

{
"exam_id": 0, 
"questions": [
    {
        "A": "A", 
        "C": "c", 
        "B": "b",      
        "D": "d", 
        "question": "A", 
        "id": 1, 
    }, 
    {
        "A": "a", 
        "C": "c", 
        "B": "B", 
        "D": "d", 
        "question": "B", 
        "id": 2, 
    },

    ....
    ....

]
}

通过使用 ListAdapter,我试图将这些值分配给一个列表。这是活动...

public class MBTExam extends ListActivity {

JSONObject result;
String exam_id;
JSONArray question_list_json;
ArrayList<String> question_list;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    Bundle extras = getIntent().getExtras();
    try {
        result = new JSONObject(extras.getString("result"));
        exam_id = result.getString("exam_id");
        question_list_json = result.getJSONArray("questions");

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        for (int i = 1; i <= question_list_json.length() ; i++) {

            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject e = question_list_json.getJSONObject(i-1);

            map.put("id", new Integer(i).toString());
            map.put("question", e.getString("question"));
            map.put("A", e.getString("A"));
            map.put("B", e.getString("B"));
            map.put("C", e.getString("C"));
            map.put("D", e.getString("D"));

            mylist.add(map);
        }

        ListAdapter adapter = new SimpleAdapter(this, 
                mylist,
                R.layout.mbt_test,
                new String[] { "id", "question", "A", "B", "C", "D" },
                new int[] { R.id.question_no, R.id.question, R.id.A, R.id.B, R.id.C, R.id.D });

        setListAdapter(adapter);

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

}
}

这是每个问题的行布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top" >

<TextView
    android:id="@+id/question_no"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignWithParentIfMissing="true" />

<TextView
    android:id="@+id/white_space"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/question_no"
    android:text="@string/white_space" />

<TextView
    android:id="@+id/question"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/white_space" />

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/question" >

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

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

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

    <RadioButton
        android:id="@+id/D"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RadioGroup>

</RelativeLayout>

这是“MBTExam”的布局

<?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:gravity="top" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/android:list">
</ListView>
<Button
    android:id="@+id/submit"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Submit"/>
</LinearLayout>

此外,除了问题,我还必须在论文末尾获得一个提交按钮。请建议我对布局代码进行必要的更改。

这是我的完整源代码

提前致谢。

4

1 回答 1

1

SimpleAdapter,就像所有好的适配器实现一样,在它的 getView 实现中回收/重用视图。当在 ListView 的另一个位置重用在其先前位置具有选择的视图时,您会看到意外的单选按钮选择。

您将需要将自定义 ViewBinder 与现有的 SimpleAdapter 一起使用,或者创建自己的 Adapter 类来控制视图的重用方式。

这个对类似问题的回答似乎是一个很好的起点:

要使您的提交按钮出现在您的布局中,请在垂直 LinearLayout 中为您的 ListView 分配“1”的布局权重值:

<?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:gravity="top"
android:orientation="vertical" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/android:list" />
<Button
    android:id="@+id/submit"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Submit"/>
</LinearLayout>
于 2012-01-11T09:55:10.660 回答