我正在開發駕駛學校測試應用程序,我需要一些幫助。從數據庫填充單選按鈕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++;
謝謝:)
我認爲非常有用的建議。我需要這種佈局,因爲在實際的考試中,你可以輕鬆地在兩個問題之間進行導航,一次只能在你的眼前展開:)圖像不是問題,它們都是20 kb左右。 – luismiyu 2012-04-09 16:43:29
您能給我舉一個單選按鈕聽衆的例子嗎? – luismiyu 2012-04-09 17:03:40
@luismiyu查看我編輯的答案。 – Luksprog 2012-04-09 17:28:36