【问题标题】:Populate radiobuttons from database android从数据库 android 填充单选按钮
【发布时间】:2012-04-21 11:03:25
【问题描述】:

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

我有这样的 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>

... x 30 次,还有一个按钮可以纠正测试并显示您是否通过了。如果您未通过 3 道或更少的问题,则您通过了测试。

问题以这种方式存储:

Id_question Id_topic Id_test Question AnswerA AnswerB AnswerC AnswerOK 图片

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

我使用数据库来填充 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());

如何填充单选按钮?查询将是“从表 WHERE id_test = 1 中选择 AnswerA、AnswerB、AnswerC”

我的最后一个问题是如何纠正测试。我认为将按下的单选按钮(我不知道具体如何)存储在数组中,然后与正确答案进行比较。查询将是“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++;

谢谢你:)

【问题讨论】:

    标签: android database radio-button populate


    【解决方案1】:

    如何填充单选按钮?查询将是“SELECT AnswerA, AnswerB, AnswerC 来自表 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 循环中将文本分配给 RadioButtons,就像您为 TextView txtenun 所做的那样。

    我的最后一个问题是如何纠正测试。我认为存储 在数组中按下单选按钮(我不知道具体如何),然后 与正确答案进行比较。

    OnCheckedChangeListener() 添加到您的所有RadioGroups。在该侦听器中,您将获得用户检查某些内容的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
    

    当用户按下nextQuestion Button 时,您将增加counter 值(您应该做一些检查,以免超过30 个问题),将光标移动到counter 位置再次然后像上面一样填充布局。当用户按下previousQuestion 你递减counter,移动光标并再次用数据填充布局。 此外,您只需将一个OnCheckedChangeListener 添加到您的RadioGroup。当该侦听器触发时,您应该将问题 id(从 counter 值中您应该能够分辨出您是哪个问题)和选定的 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。
    • 你能给我一个单选按钮监听器的例子吗?
    • 这将添加一个“0”、“1”或“2”,我可以将它们与从我的数据库中提取的“0”、“1”或“2”进行比较,对吧?谢谢@Luksprog。
    • @luismiyu 是的(但这些是 integer 值,数据库中的 AnswerOK 也是如此)。请记住,如果用户跳过其中一个问题,它会弄乱您的正确答案ArrayList,所以要小心。
    • 我强制第一个单选按钮始终使用 'rg.check(rb1);'在循环中,所以我认为这将解决跳过的响应问题...我无法从侦听器访问“响应”ArrayList,它必须是最终的。这样做会导致强制关闭!
    【解决方案2】:

    根据@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());
    

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 2013-05-01
      • 2012-03-19
      相关资源
      最近更新 更多