2012-04-08 70 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());