1、Qusetion类:
每一个问题包含:问题描述、答案a、答案b、答案c、答案d、答案、id号、解释、被选择的答案。
package com.example.exam;
public class Question {public String question;
public String answerA;
public String answerB;
public String answerC;
public String answerD;
public int answer;
public String explaination;
public int ID;
public int selectedAnser;
}
2、DBService数据库服务类
每一个问题是一个对象,每一个为题对象对应数据库中的每一条记录。
把数据库中的每一条记录,读取出来的每一道题放在list 里面,list是存放对象的数组。
package com.example.exam;
import java.util.ArrayList;
import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DBService {
private SQLiteDatabase db;
public DBService(){
db=SQLiteDatabase.openDatabase("/data/data/com.example.exam/databases/question.db", null, SQLiteDatabase.OPEN_READONLY);
}
public List<Question> getQuestions(){
List<Question> list=new ArrayList<Question>();
Cursor cursor=db.rawQuery("select * from question", null);
if(cursor.getCount()>0){
cursor.moveToFirst();
int count=cursor.getCount();
for(int i=0;i<count;++i){
cursor.moveToPosition(i);
Question question=new Question();
question.question=cursor.getString(cursor.getColumnIndex("question"));
question.answerA=cursor.getString(cursor.getColumnIndex("answerA"));
question.answerB=cursor.getString(cursor.getColumnIndex("answerB"));
question.answerC=cursor.getString(cursor.getColumnIndex("answerC"));
question.answerD=cursor.getString(cursor.getColumnIndex("answerD"));
question.answer=cursor.getInt(cursor.getColumnIndex("answer"));
question.ID=cursor.getInt(cursor.getColumnIndex("ID"));
question.explaination=cursor.getString(cursor.getColumnIndex("explaination"));
question.selectedAnser=-1;
list.add(question);
}
}
return list;
}
}
3、ExamActivity测试窗体
代码实现:
package com.example.exam;
import java.util.ArrayList;
import android.app.AlertDialog;
import java.util.List;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class ExamActivity extends Activity {
private int count;
private int current;
private boolean wrongMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam);
DBService dbService=new DBService();
final List<Question>list=dbService.getQuestions();
count=list.size();
current=0;
//问题描述
final TextView tv_question=(TextView) findViewById(R.id.question);
final RadioButton[]radioButtons=new RadioButton[4];
//四个选项按钮
radioButtons[0]=(RadioButton) findViewById(R.id.AnswerA);
radioButtons[1]=(RadioButton) findViewById(R.id.AnswerB);
radioButtons[2]=(RadioButton) findViewById(R.id.AnswerC);
radioButtons[3]=(RadioButton) findViewById(R.id.AnswerD);
//两个前一题和后一题的按钮
Button btn_next=(Button) findViewById(R.id.btn_next);
Button btn_previous=(Button) findViewById(R.id.btn_previous);
final TextView tv_explaination=(TextView) findViewById(R.id.explaination);
final RadioGroup radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
//将第一题先赋值
Question q=list.get(0);
tv_question.setText(q.question);
tv_explaination.setText(q.explaination);
radioButtons[0].setText(q.answerA);
radioButtons[1].setText(q.answerB);
radioButtons[2].setText(q.answerC);
radioButtons[3].setText(q.answerD);
//下一题
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(current<count-1){
current++;
//list是存储对象的数组,通过get(i)获得每个对象
Question q=list.get(current);
//将所用的question对象各个属性归位。
tv_question.setText(q.question);
radioButtons[0].setText(q.answerA);
radioButtons[1].setText(q.answerB);
radioButtons[2].setText(q.answerC);
radioButtons[3].setText(q.answerD);
tv_explaination.setText(q.explaination);
radioGroup.clearCheck();
if(q.selectedAnser!=-1){
radioButtons[q.selectedAnser].setChecked(true);
}
}
else if(current == count - 1 && wrongMode == true)
{
new AlertDialog.Builder(ExamActivity.this)
.setTitle("提示")
.setMessage("已经到达最后一题,是否退出?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ExamActivity.this.finish();
}
})
.setNegativeButton("取消", null)
.show();
}
else
{
final List<Integer> wrongList = checkAnswer(list);
if(wrongList.size() == 0)
{
new AlertDialog.Builder(ExamActivity.this)
.setTitle("提示")
.setMessage("恭喜你全部回答正确!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ExamActivity.this.finish();
}
})
.show();
}
new AlertDialog.Builder(ExamActivity.this)
.setTitle("提示")
.setMessage("您答对了" + (list.size() - wrongList.size()) +
"道题目,答错了" + wrongList.size() + "道题目。是否查看错题?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
wrongMode = true;
List<Question> newList = new ArrayList<Question>();
for (int i = 0; i < wrongList.size(); i++) {
newList.add(list.get(wrongList.get(i)));
}
list.clear();
for (int i = 0; i < newList.size(); i++) {
list.add(newList.get(i));
}
current = 0;
count = list.size();
Question q = list.get(current);
tv_question.setText(q.question);
radioButtons[0].setText(q.answerA);
radioButtons[1].setText(q.answerB);
radioButtons[2].setText(q.answerC);
radioButtons[3].setText(q.answerD);
tv_explaination.setText(q.explaination);
tv_explaination.setVisibility(View.VISIBLE);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ExamActivity.this.finish();
}
})
.show();
}
}
});
//前一个按钮
btn_previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(current>0){
current--;
Question q=list.get(current);
tv_question.setText(q.question);
radioButtons[0].setText(q.answerA);
radioButtons[1].setText(q.answerB);
radioButtons[2].setText(q.answerC);
radioButtons[3].setText(q.answerD);
tv_explaination.setText(q.explaination);
radioGroup.clearCheck();
if(q.selectedAnser!=-1){
radioButtons[q.selectedAnser].setChecked(true);
}
}
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int i=0;i<4;++i){
if(radioButtons[i].isChecked()==true){
list.get(current).selectedAnser=i;
break;
}
}
}
});
}
private List<Integer> checkAnswer(List<Question> list ){
List<Integer> wrongList=new ArrayList<Integer>();
for(int i=0;i<list.size();++i)
{
if(list.get(i).answer!=list.get(i).selectedAnser)
{
wrongList.add(i);
}
}
return wrongList;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.exam, menu);
return true;
}
}
本窗体的UI:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ExamActivity" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:text="Question Desciption"
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:text="Answer A"
android:id="@+id/AnswerA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<RadioButton
android:text="Answer B"
android:id="@+id/AnswerB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<RadioButton
android:text="Answer C"
android:id="@+id/AnswerC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<RadioButton
android:text="Answer D"
android:id="@+id/AnswerD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RadioGroup>
<TextView
android:visibility="invisible"
android:id="@+id/explaination"
android:text="Question Expaination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="7"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_previous"
android:text="上一题"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="下一题"
android:id="@+id/btn_next"
/>
</LinearLayout>
</LinearLayout>
截图如下:
4、主窗体 MainActivity
package com.example.exam;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String DB_PATH = "/data/data/com.example.exam/databases/";
String DB_NAME = "question.db";
if((new File(DB_PATH + DB_NAME).exists()) == false)
{
File dir = new File(DB_PATH);
if (!dir.exists())
{
dir.mkdir();
}
try {
InputStream is = getBaseContext().getAssets().open(DB_NAME);
OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);
byte[] buffer = new byte[1024];
int length;
while((length = is.read(buffer)) > 0)
{
os.write(buffer, 0, length);
}
os.flush();
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ExamActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:text="答题" />
</RelativeLayout>
总结: