ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

从数据库Android填充单选按钮

2019-12-01 14:28:53  阅读:210  来源: 互联网

标签:populate android database radio-button


我正在开发驾驶学校测试应用程序,我需要一些帮助.

我有这样的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问题答案A答案B答案C答案OK图像

> 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中选择答案A,答案B,答案C”

我的最后一个问题是如何纠正测试.我认为将按下的单选按钮(我不知道怎么做)存储在数组中,然后与正确答案进行比较.该查询将是“从表WHERE id_test = 1中选择SELECT AnswerOK”.

例:

一系列已回答的问题:
    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++;

谢谢 :)

解决方法:

How can I populate radiobuttons? The query would be “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一样,将文本分配给RadioButtons.

And my last question is how can I correct the test. I think on storing
radiobutton pressed (I don’t know exactly how) in an array and then
compare with correct answers.

将OnCheckedChangeListener()添加到所有RadioGroup中.在该侦听器中,您将获得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按钮时,您将增加计数器的值(您应进行一些检查,以免使30个问题不至于过大),再次将光标移至计数器的位置,然后像上面那样填充布局.当用户按下上一个问题递减计数器时,移动光标并再次使用数据填充布局.
另外,您将只向OnRadiedGroup添加一个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
            }
      } 
});

标签:populate,android,database,radio-button
来源: https://codeday.me/bug/20191201/2081283.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有