2012-04-08 14 views
3

私は運転スクールのテストアプリを開発しています。私はいくつかの助けが必要です。データベースからラジオボタンを入力してくださいandroid

<!-- 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回とテストを修正し、あなたが渡されたかどうかを示すだろうボタン:

は、私は、XMLのようなファイルを持っています。あなたは3つ以下の質問に失敗したときにテストに合格します。

質問は、このように格納されます

Id_question Id_topic Id_test質問AnswerA AnswerB AnswerC AnswerOK画像

  • Id_question Id_topicとid_testは、整数フィールド
  • ある質問と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()); 

ラジオボタンはどのように設定できますか?クエリは "SELECT AnswerA、AnswerB、テーブルWHERE id_test = 1からのAnswerC"になります

そして私の最後の質問は、どのようにしてテストを修正できるかです。私は、ラジオボタンを押したまま(私は正確にはどういうわけか)配列に格納して、正しい答えと比較すると思います。クエリは "テーブルから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++; 

いただきありがとうございます:)

答えて

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" 

ます持っている:あなたはTextView txtenunのために行ったよう

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

は、そのwhileループにRadioButton秒にテキストを割り当てます。

私の最後の質問は、どのようにしてテストを修正できるかです。私は ラジオボタンを押して格納していると思います(正確にはわかりません)、配列内で と正しい答えを比較します。

RadioGroupにはすべてOnCheckedChangeListener()を追加してください。そのリスナーでは、ユーザーが何かをチェックしたRadioGroupと、チェックされたRadioButtonのIDを取得します。それを使って正しい答えの配列を構築してください。

いくつかのアドバイス:

たぶん、あなたはあなたのレイアウトや、あなたの現在のアプローチを変更する必要があります。ユーザーが30個の質問レイアウトをスクロールさせることは良い考えではないかもしれませんし、多くのリソースを読み込んでいるかもしれませんが、特定の質問になるまで実際には表示されません(私はあなたのイメージのサイズ、彼らが小さい場合、これはおそらく問題ではない)。他の選択肢は、ListViewレイアウトか、ユーザーがどの質問に応じてオンデマンドで入力するかという1つの質問の単純なレイアウトです。私の助言が第二の選択肢になるだろう。このオプションは、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 

nextQuestionButtonを押すと、counterの値が増えます(30問を外に出さないようにチェックする必要があります)。counterの位置にカーソルを戻してから、上記のようにレイアウトを入力します。ユーザがpreviousQuestionを押すと、counterが減ります。カーソルを移動し、再度レイアウトをデータに入力します。 またOnCheckedChangeListenerRadioGroupに追加します。そのリスナーが起動すると、質問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 
      } 
     } 
}); 
+0

私は非常に便利なあなたのアドバイスを見つけます。私はこの種のレイアウトが必要です。なぜなら、実際の試験では、あなたの目の前で一瞬にして質問間を簡単に移動できるからです:)画像は問題ではない、それらはすべて約20kbです。 – luismiyu

+0

ラジオボタンのリスナーの例を教えてください。 – luismiyu

+0

@luismiyu私の編集された答えを見てください。 – Luksprog

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()); 
関連する問題