Populating radio objects from android database

I am developing an application for driving school exam and I need help.

I have xml file:

<!-- 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 times and a button that will correct the test and show if you passed or not. You take the test if you fail 3 questions or less.

Questions are stored as follows:

Id_question Id_topic Id_test Question Answer Answer AnswerC AnswerOK Image

  • Id_question Id_topic and id_test are integer fields
  • Q&A A-C are text boxes
  • AnswerOK is an integer field. If AnswerA OK = 1, AnswerB = 2, AnswerC = 3
  • Image is the name of the associated image retrieved from the resource folder.

I am using a database to populate each field in the xml. Images and questions are retrieved from the database as follows:

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

      

How can I fill radio lenses? The query will be "SELECT AnswerA, AnswerB, AnswerC from WHERE table id_test = 1"

And my last question is how can I fix the test. I think that when storing the superimposed radiobutton (I don't know exactly how) in an array, then compare to the correct answers. The query will be "SELECT AnswerOK from WHERE table id_test = 1".

Example:

Array of answers to questions: 1 1 1 2 2 3 2 3 1 3 2 ... Array of correct answers: 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++;

      

Thank:)

+3


source to share


2 answers


How can I fill radio lenses? The query will be "SELECT AnswerA, AnswerB, AnswerC from WHERE table id_test = 1"

You probably want to select those values ​​directly in the first query, so instead of:

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

      

will have:

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

      

Then, in this loop, while

assign text RadioButton

as you did to TextView txtenun

.

And my last question is how can I fix the test. I think when storing (I don't know for sure) in an array and then compare with the correct answers.

Add OnCheckedChangeListener()

to all your RadioGroup

s. In this listener, you will get RadioGroup

where the user checked something and the ID of the checked one RadioButton

. Use this to build an array of correct answers.

Some tips:

Perhaps you should change the layout and your current approach. Giving the user a scroll through 30 question templates might not be such a good idea, also you are loading a lot of resources, although the user won't actually see them until they get to this particular question (I don't know the size of your image, if they are small it probably isn't problem). Other alternatives are a mock ListView

or a simple one-question mock that you fill out on demand, depending on what questions the user is on. My advice would be the second option. This parameter will also help you avoid method calls getIdentifier

, this method is slower than findViewById

that and should be avoided. The boot is a layout:



<?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>

      

You will query for the entire question in this test (including all data) and start with a static field int counter = 0

. When the application is launched, you will move the cursor to the counter value (which is 0 at the beginning):

c.moveToPosition(counter);

      

and use the values ​​from that line to fill in the above question layout:

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

      

When the user clicks the button nextQuestion

Button

, you increment the value counter

(you have to do some validation so you don't go overboard with 30 questions), move the cursor to position counter

again, and then fill the layout as above. When the user clicks on the previous query, you zoom out counter

, move the cursor, and populate the layout with data again. Also you will only add one OnCheckedChangeListener

to yours RadioGroup

. When this listener fires, you have to store the question id (from the value counter

you should specify for which question) and selected RadioButton

in the data structure (possibly HashMap

), get the correct answers for the test from the database, and see if the user is a good driver.

Also you could (possibly) optimize your database.

Edit:

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

      

+3


source


As per @ Luksprog answer, this is my final runtime:



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

      

0


source







All Articles