2012-04-08 72 views
3

我正在开发驾驶学校测试应用程序,我需要一些帮助。从数据库填充单选按钮android

我的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问题AnswerA AnswerB AnswerC AnswerOK图片

  • Id_question Id_topic和id_test是整场
  • 问题和AnswerA-C是文本字段
  • AnswerOK是一个整数字段。如果AnswerA = 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()); 

我怎么能填充单选按钮?该查询将是“选择答案A,答案B,表WHERE id_test = 1的答案”

而我最后的问题是我该如何纠正测试。我想在数组中存储单选按钮(我不知道如何),然后与正确的答案进行比较。该查询将是“从表WHERE id_test = 1 SELECT SELECTOK”。

例子:

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

谢谢:)

回答

3

我怎么能填充单选按钮?查询是 “从表中选择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循环文本分配给RadioButton就像你为TextView txtenun

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

OnCheckedChangeListener()添加到您的所有RadioGroup s。在那个听众中,你会得到RadioGroup,其中用户检查了一些东西,并检查了RadioButton的ID。用它来构造正确答案的数组。

一些建议:

也许你应该修改你的布局和当前的做法。让用户滚动你的30个问题布局可能不是一个好主意,同时你也在加载很多资源,尽管用户直到遇到特定问题时才会真正看到它们(我不知道图像的大小,如果它们很小,这可能不是问题)。您的其他替代方案是ListView布局或根据需求填充的一个问题的简单布局,具体取决于用户所处的问题。我的建议是第二个选择。该选项也将帮助您避免方法调用,此方法比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 

当用户按下nextQuestionButton时,您将增加counter的值(您应该进行一些检查以免超出30个问题),将光标再次移动到counter的位置,然后填充上面的布局。当用户按下previousQuestion时,你减少counter,移动光标并再次用数据填充布局。 此外,您只会将OnCheckedChangeListener添加到您的RadioGroup。当该听众触发时,应该在数据结构(可能是HashMap)中存储问题ID(来自counter值,您应该能够告诉您是哪个问题)和选定的RadioButton,从中检索测试的正确答案数据库,看看用户是否是一个好的驱动程序。

你也可以(可能)优化你的数据库。

编辑:

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 
      } 
     } 
}); 
+0

我认为非常有用的建议。我需要这种布局,因为在实际的考试中,你可以轻松地在两个问题之间进行导航,一次只能在你的眼前展开:)图像不是问题,它们都是20 kb左右。 – luismiyu 2012-04-09 16:43:29

+0

您能给我举一个单选按钮听众的例子吗? – luismiyu 2012-04-09 17:03:40

+0

@luismiyu查看我编辑的答案。 – Luksprog 2012-04-09 17:28:36

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());