从数据库中填充Android单选按钮

3
我可以帮助你翻译以下内容,它与IT技术有关。请注意,我将仅翻译文本,但不会提供代码或其他技术支持。

我正在开发一款驾校考试应用程序,需要一些帮助。

我有一个像这样的XML文件:

<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget28"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#AAC1E2" >

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

        <ImageView
            android:id="@+id/i1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />

        <TextView
            android:id="@+id/e1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enun"
            android:textAppearance="?android:attr/textAppearanceMedium" />

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

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

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

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

         <ImageView
            android:id="@+id/i2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />

        <TextView
            android:id="@+id/e2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enun"
            android:textAppearance="?android:attr/textAppearanceMedium" />

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

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

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

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

...30次并有一个按钮来纠正测试并展示你是否通过。当你答错3个或更少的问题时,你就通过了这个测试。

问题存储方式如下:

Id_question Id_topic Id_test Question AnswerA AnswerB AnswerC AnswerOK Image

  • Id_question、Id_topic和id_test是整数字段
  • Question和AnswerA-C是文本字段
  • AnswerOK是整数字段。如果AnswerA是正确的= 1,AnswerB = 2,AnswerC = 3
  • Image是相关图像的名称,从资源文件夹中提取。

我使用数据库来填充xml中的每个字段。图像和问题是按以下方式从数据库中提取的:

<!-- language: lang-java -->
    Cursor c = adbh.query(bundle.getString("test")); 
    //"test"-->"SELECT question, image FROM table WHERE id_test = 1"

        Integer i = 1;

        if (c.moveToFirst()) {
            do {
                String path = c.getString(index);
                String nimage = "i"+i;
                int idImage = getResources().getIdentifier(nimage, "id", this.getPackageName());
                ImageView image = (ImageView) findViewById(idImage);
                int idDrawable = getResources().getIdentifier(ruta, "drawable", this.getPackageName());
                image.setImageResource(idDrawable);

                String nenun = "e"+i;
                String enun = c.getString(anotherIndex);
                int idEnun = getResources().getIdentifier(nenun, "id", this.getPackageName());
                TextView txtenun = (TextView) findViewById(idEnun);
                txtenun.setText(enun);

                i++;
            } while (c.moveToNext());

如何填充单选按钮?查询语句为:“SELECT AnswerA,AnswerB,AnswerC from table WHERE id_test = 1”。
最后一个问题是如何纠正测试。我想在数组中存储单选按钮的状态(我不知道具体如何实现),然后与正确答案进行比较。查询语句为:“SELECT AnswerOK from table WHERE id_test = 1”。
示例:
回答问题的数组: 1 1 1 2 2 3 2 3 1 3 2… 正确答案的数组: 3 2 1 2 2 3 2 3 2 2 2…
<!-- language: lang-java -->
    for (int i=0; i<30; i++)
    if (a[i] == b[i])
    numberOfRightAnswers++;

谢谢您:)
2个回答

3

如何填充单选按钮?查询应为“SELECT AnswerA,AnswerB,AnswerC from table WHERE id_test = 1”

可能希望直接在第一次查询中选择这些值,所以不是:

Cursor c = adbh.query(bundle.getString("test")); 
    //"test"-->"SELECT question, image FROM table WHERE id_test = 1"

将会拥有:

Cursor c = adbh.query(bundle.getString("test")); 
    //"test"-->"SELECT question, AnswerA,  AnswerB, AnswerC, image FROM table WHERE id_test = 1"

然后在那个while循环中,像你为TextView txtenun所做的一样,将文本赋值给RadioButton

我最后一个问题是如何纠正测试。我想通过将选中的单选按钮(我不确定如何操作)存储到数组中,然后与正确答案进行比较。

为所有的RadioGroup添加OnCheckedChangeListener()。在该监听器中,您将得到用户选中某些内容的RadioGroup和所选RadioButton的ID。使用这些来构建正确答案的数组。

一些建议:

也许你应该修改你的布局和当前的方法。让用户滚动你的30道题可能不是一个好主意,而且你正在加载许多资源,尽管用户直到进入特定问题之前实际上并不会看到它们(如果它们很小,这可能不是一个问题)。你的其他选择是ListView布局或一个简单的问题布局,根据用户所在的问题填充它。我的建议是第二个选项。这个选项还将帮助你避免getIdentifier方法调用,这个方法比findViewById慢,应该被避免。下面是一个布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#AAC1E2" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/questionImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/questionText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enun"
            android:textAppearance="?android:attr/textAppearanceMedium" />

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

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

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

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="3" >

            <Button
                android:id="@+id/previousQuestion"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="previous" />

            <Button
                android:id="@+id/validateTest"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="validate" />

            <Button
                android:id="@+id/nextQuestion"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="next" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>

你将查询该测试中所有的问题(包括所有数据),并从静态字段int counter = 0开始。当应用程序启动时,您将将游标移动到计数器值(在开始时为0):
c.moveToPosition(counter);

使用该行的值来填充上述问题布局:

ImageView questionImage = (ImageView) findViewById(R.id.questionImage);
int idDrawable = getResources().getIdentifier(ruta, "drawable", this.getPackageName());
   questionImage.setImageResource(idDrawable);

String enun = c.getString(anotherIndex);
TextView txtenun = (TextView) findViewById(R.id.questionText);
txtenun.setText(enun);
// will do the same to populate the other parts of the question from the cursor

当用户点击“下一题”按钮时,您将增加计数器的值(应进行一些检查以确保不会超过30个问题),将光标移动到计数器位置并像上面那样填充布局。当用户按“上一题”时,您要减少计数器的值,移动光标,然后再次使用数据填充布局。
此外,您只需向RadioGroup添加一个OnCheckedChangeListener。当该侦听器触发时,您应该将问题ID(从计数器值中,您应该能够知道您在哪个问题上)和所选的RadioButton存储在数据结构中(可能是HashMap),从数据库中检索测试的正确答案,并查看用户是否是良好驾驶员。
另外,您还可以(可能)优化数据库。
private ArrayList<Integer> responses = new ArrayList<Integer>(); // this will hold the number of the correct answer. Note that this will work if the user answers question one after the other
// if the user skips question you'll get in trouble, this is one of the reasons why your design is not so good.

String rg = "radioGroup" + i;
int idRg = getResources().getIdentifier(rg, "id", this.getPackageName());
RadioGroup radioGroup = (TextView) findViewById(idRg);
radioGroup.setOnChekedChangeListener(new OnCheckedChangeListener(){
       public void onCheckedChanged (RadioGroup group, int checkedId) {
            if (group.getChildAt(0).getId() == checkedId) {
               responses.add(0); // the first answer was checked
            } else if (group.getChildAt(1).getId() == checkedId) {
               responses.add(1); // the first answer was checked
            } else if (group.getChildAt(2).getId() == checkedId) {
               responses.add(2); // the first answer was checked
            }
      } 
});

我发现你的建议非常有用。我需要这种布局,因为在实际考试中,你可以轻松地在问题之间导航,一次性将所有问题都放在眼前 :) 图像不是问题,它们都大约20 kb。 - luismiyu
我在循环中使用了“rg.check(rb1);”来强制第一个单选按钮始终被选中,因此我认为这可以解决跳过响应的问题... 我无法从监听器中访问“responses” ArrayList,它必须是final。这样做会导致强制关闭! - luismiyu
@luismiyu 将responsesArrayList作为类中的一个字段。 - user
@luismiyu 我不知道你是否能够理解我的回答,但是这里有一个小的示例活动 http://pastebin.com/NFDKaMR1 你可以看一下。 - user
@luismiyu,检查用户答案不应该太难。 - user
显示剩余5条评论

0

根据@Luksprog的回答,这是我的最终工作循环:

Cursor c = adbh.query(bundle.getString("test"));

    Integer i = 1;

    if (c.moveToFirst()) {
        do {
            String ruta = c.getString(4);
            String nimage = "i" + i;
            int idImage = getResources().getIdentifier(nimage, "id",
                    this.getPackageName());
            ImageView image = (ImageView) findViewById(idImage);
            int idDrawable = getResources().getIdentifier(ruta, "drawable",
                    this.getPackageName());
            image.setImageResource(idDrawable);

            String rgname = "radioGroup"+i;
            int idRg = getResources().getIdentifier(rgname, "id",
                    this.getPackageName());
            RadioGroup rg = (RadioGroup) findViewById(idRg);
            rg.removeAllViews();
            RadioButton rb1 = new RadioButton(this);
            rb1.setId(i);
            rb1.setText(c.getString(1));
            rg.addView(rb1);
            RadioButton rb2 = new RadioButton(this);
            rb2.setId(i+1);
            rb2.setText(c.getString(2));
            rg.addView(rb2);
            RadioButton rb3 = new RadioButton(this);
            rb3.setId(i+2);
            rb3.setText(c.getString(3));
            rg.addView(rb3);


            String nenun = "e" + i;
            String enun = c.getString(0);
            int idEnun = getResources().getIdentifier(nenun, "id",
                    this.getPackageName());
            TextView txtenun = (TextView) findViewById(idEnun);
            txtenun.setText(enun);

            i++;
        } while (c.moveToNext());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接