本android项目是致力于建立一个本地的sqlite数据库,然后存储考试的题目,android应用调用sqlite数据库中的数据,生成相应的答题界面。该应用参考极客学院安卓答题系统。
下面是代码解析
项目主要是有界面是activity_exam.xml 和 activity_index.xml组成
(1)activity_exam.xml 展示界面
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<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="com.exam.administrator.exam.IndexActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Exam"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp" />
</RelativeLayout>
(2)activity_question.xml界面和代码如下
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.exam.administrator.exam.ExamActivity">
<ScrollView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Question Description"
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="AnswerA"
android:id="@+id/answerA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:text="AnswerB"
android:id="@+id/answerB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:text="AnswerC"
android:id="@+id/answerC"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:text="AnswerD"
android:id="@+id/answerD"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:text="Question Exlaination"
android:visibility="invisible"
android:id="@+id/explanation"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_weight="7"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/previous"
android:text="previous"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/next"
android:text="next"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
(3)逻辑处理部分
接下来就是4个逻辑处理部分了
DBservice 负责数据库数据的存取
Question 为自定义数据类型,存储信息
在indexActivity类中创建sqlite数据库,并设置startExam按钮的相应事件,跳转到答题界面
examActivity类为题目展示阶段,将数据库中的问题取出展示到android应用上
DBService.java 代码
package com.exam.administrator.exam;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/8/15.
*/
public class DBService {
private SQLiteDatabase db;
public DBService(){
db = SQLiteDatabase.openDatabase("/data/data/com.exam.administrator.exam/databases/question.db",null, SQLiteDatabase.OPEN_READWRITE);
}
public List<Question> getQuestion(){
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.explanation = cursor.getString(cursor.getColumnIndex("explanation"));
question.selectedAnswer = -1;
list.add(question);
}
}
return list;
}
}
examActivity.java代码
package com.exam.administrator.exam;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ExamActivity extends AppCompatActivity {
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.getQuestion();
count = list.size();
current = 0;
wrongMode = false;
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.next);
Button btn_previous =(Button)findViewById(R.id.previous);
final TextView tv_explanation = (TextView)findViewById(R.id.explanation);
final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
Question q = list.get(0);
tv_question.setText(q.question);
tv_explanation.setText(q.explanation);
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 view) {
if(current < count -1){
current++;
String string = "" + count;
//Toast.makeText(ExamActivity.this, string,Toast.LENGTH_SHORT).show();
Question q = list.get(current);
tv_question.setText(q.question);
radioGroup.clearCheck();
radioButtons[0].setText(q.answerA);
radioButtons[1].setText(q.answerB);
radioButtons[2].setText(q.answerC);
radioButtons[3].setText(q.answerD);
tv_explanation.setText(q.explanation);
if(q.selectedAnswer != -1){
radioButtons[q.selectedAnswer].setChecked(true);
}
}else if(current == count-1 && wrongMode == true){
new AlertDialog.Builder(ExamActivity.this)
.setTitle("Remider")
.setMessage("End of the question, quit or not?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
ExamActivity.this.finish();
}
}).setNegativeButton("No", null)
.show();
}
else{
// Toast.makeText(ExamActivity.this, "Yes",Toast.LENGTH_SHORT).show();
final List<Integer> wrongList = checkAnswer(list);
if(wrongList.size() == 0){
new AlertDialog.Builder(ExamActivity.this)
.setTitle("reminder")
.setMessage("All are Right")
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
ExamActivity.this.finish();
}
}).show();
}else{
new AlertDialog.Builder(ExamActivity.this)
.setTitle("reminder")
.setMessage((list.size()-wrongList.size()) + " right answers. " +
wrongList.size() + " wrong answers. Do you want to review?")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
wrongMode = true;
List<Question> newList = new ArrayList<Question>();
for(int j = 0; j < wrongList.size(); j++){
newList.add(list.get(wrongList.get(j)));
}
list.clear();
for(int j = 0; j < newList.size(); j++){
list.add(newList.get(j));
}
current = 0;
count = list.size();
Question q = list.get(current);
tv_question.setText(q.question);
radioGroup.clearCheck();
radioButtons[0].setText(q.answerA);
radioButtons[1].setText(q.answerB);
radioButtons[2].setText(q.answerC);
radioButtons[3].setText(q.answerD);
tv_explanation.setText(q.explanation);
tv_explanation.setVisibility(View.VISIBLE);
if(q.selectedAnswer != -1){
radioButtons[q.selectedAnswer].setChecked(true);
}
}
}).setNegativeButton("NO", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i){
ExamActivity.this.finish();
}
}).show();
}}
}
});
btn_previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(current > 0){
current--;
Question q = list.get(current);
radioGroup.clearCheck();
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_explanation.setText(q.explanation);
if(q.selectedAnswer != -1){
radioButtons[q.selectedAnswer].setChecked(true);
}
}
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
for(int j = 0; j < 4; j++){
if(radioButtons[j].isChecked() == true){
list.get(current).selectedAnswer = j+1;
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).selectedAnswer){
wrongList.add(i);
}
}
return wrongList;
}
}
indexActivity.java
package com.exam.administrator.exam;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class IndexActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
// open or create question database
SQLiteDatabase db = openOrCreateDatabase("question.db", Context.MODE_PRIVATE, null);
db.execSQL("DROP TABLE IF EXISTS question");
// create question table
db.execSQL("CREATE TABLE `question` (`ID` INTEGER, `question` TEXT, `answerA` TEXT, `answerB` TEXT, `answerC` TEXT, `answerD` TEXT, `answer` NUMERIC, `explanation` TEXT, PRIMARY KEY(ID))");
Question question1 = new Question();
Question question2 = new Question();
Question question3 = new Question();
question1.question = "Oracle中VARCHAR2类型的最大长度是";
question1.answerA = "4000";
question1.answerB = "3000";
question1.answerC = "1000";
question1.answerD = "2000";
question1.answer = 1;
question1.explanation = "4000";
question1.ID= 1;
question2.question = "在Java中,负责对字节代码解释执行的是";
question2.answerA = "应用服务器";
question2.answerB = "虚拟机";
question2.answerC = "垃圾回收器";
question2.answerD = "编译器";
question2.answer = 2;
question2.explanation = "虚拟机";
question2.ID = 2;
question3.question = "一个栈的输入序列为1 2 3 4 5,则下列序列中不可能是栈得输出序列的是";
question3.answerA = "5 4 1 3 2";
question3.answerB = "2 3 4 1 5";
question3.answerC = "1 5 4 3 2";
question3.answerD = "2 3 1 4 5";
question3.answer = 1;
question3.explanation = "5 4 1 3 2";
question3.ID = 3;
db.execSQL("INSERT INTO question VALUES(?,?,?,?,?,?,?,?)", new Object[]{question1.ID, question1.question,question1.answerA,question1.answerB,question1.answerC,question1.answerD, question1.answer, question1.explanation});
db.execSQL("INSERT INTO question VALUES(?,?,?,?,?,?,?,?)", new Object[]{question2.ID, question2.question,question2.answerA,question2.answerB,question2.answerC,question2.answerD, question2.answer, question2.explanation});
db.execSQL("INSERT INTO question VALUES(?,?,?,?,?,?,?,?)", new Object[]{question3.ID, question3.question,question3.answerA,question3.answerB,question3.answerC,question3.answerD, question3.answer, question3.explanation});
/*Cursor c= db.rawQuery("select * from question",null);
while(c.moveToNext()){
int ID = c.getInt(c.getColumnIndex("ID"));
String question = c.getString(c.getColumnIndex("question"));
String answerA = c.getString(c.getColumnIndex("answerA"));
String answerB = c.getString(c.getColumnIndex("answerB"));
String answerC = c.getString(c.getColumnIndex("answerC"));
String answerD = c.getString(c.getColumnIndex("answerD"));
Log.i("db","ID=>" + ID + ", quesiton=>" + question + ", answerA=>" + answerA + ", answerB=>" + answerB + ", answerC=>" + answerC + ", answerD=>" + answerD );
System.out.println(question);
}*/
//c.close();
db.close();
Button btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent = new Intent(IndexActivity.this, ExamActivity.class);
startActivity(intent);
}
});
}
}
Question.java代码
package com.exam.administrator.exam;
/**
* Created by Administrator on 2016/8/15.
*/
public class Question {
public String question;
public String answerA;
public String answerB;
public String answerC;
public String answerD;
public int answer;
public String explanation;
public int ID;
public int selectedAnswer;
}