3

私は自動車教習所のテストアプリを開発していますが、助けが必要です。

私はこのような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_questionId_topicId_test質問AnswerAAnswerBAnswerCAnswerOK画像

  • Id_questionId_topicとid_testは整数フィールドです
  • 質問と回答A-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());

ラジオボタンを設定するにはどうすればよいですか?クエリは「SELECTAnswerA、AnswerB、AnswerC from table WHERE id_test=1」になります。

そして私の最後の質問は、どうすればテストを修正できるかということです。押されたラジオボタン(正確な方法はわかりません)を配列に格納してから、正解と比較することを考えています。クエリは「SELECTAnswerOKfromtable 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++;

ありがとうございました :)

4

2 に答える 2

3

ラジオボタンを設定するにはどうすればよいですか?クエリは「SELECTAnswerA、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にテキストをsに割り当てます。RadioButtonTextView txtenun

そして私の最後の質問は、どうすればテストを修正できるかということです。押されたラジオボタン(正確な方法はわかりません)を配列に格納してから、正解と比較することを考えています。

OnCheckedChangeListener()すべてのにを追加しますRadioGroup。そのリスナーRadioGroupでは、ユーザーが何かをチェックした場所とチェックされたIDを取得しますRadioButton。これを使用して、正解の配列を作成します。

いくつかのアドバイス:

多分あなたはあなたのレイアウトとあなたの現在のアプローチを修正するべきです。ユーザーに30の質問のレイアウトをスクロールさせるのはあまり良い考えではないかもしれません。また、特定の質問に到達するまでユーザーには実際には表示されませんが、多くのリソースを読み込んでいます(画像のサイズはわかりませんが、それらが小さい場合、これはおそらく問題ではありません)。他の選択肢はListView、ユーザーがどの質問にいるかに応じて、オンデマンドで入力する1つの質問のレイアウトまたは単純なレイアウトです。私のアドバイスは2番目の選択肢です。このオプションは、メソッド呼び出しを回避するのにも役立ちます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、カーソルを移動して、レイアウトにデータを再度入力します。また、に1つだけ追加OnCheckedChangeListenerしますRadioGroup。そのリスナーが起動したら、質問ID(counter自分がどの質問であるかがわかるはずの値から)と選択RadioButtonしたデータ構造(おそらくa 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
            }
      } 
});
于 2012-04-09T08:27:07.887 に答える
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());
于 2012-04-09T16:59:49.407 に答える