question.selectedAnswer=-1;
list.add(question);
}
}
return list;
}
}
ExamActivity:
package com.example.lenovo.exam;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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 java.util.ArrayList;
import java.util.List;
/**
* Created by lenovo on 2017/12/4.
*/
public class ExamActivity extends Activity {
private int count;
private int current;
private boolean wrongMode;//标志变量,判断是否进入错题模式
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam);
DBService dbService = new DBService();
final List list = dbService.getQuestion();
count = list.size();
current = 0;
wrongMode=false;//默认情况
final TextView tv_question = findViewById(R.id.question);
final RadioButton[] radioButtons = new RadioButton[4];
radioButtons[0] = findViewById(R.id.answerA);
radioButtons[1] = findViewById(R.id.answerB);
radioButtons[2] = findViewById(R.id.answerC);
radioButtons[3] = findViewById(R.id.answerD);
Button btn_previous = findViewById(R.id.btn_previous);
Button btn_next = findViewById(R.id.btn_next);
final TextView tv_explaination = findViewById(R.id.explaination);
final 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 view) {
if (current < count - 1) {//若当前题目不为最后一题,点击next按钮跳转到下一题;否则不响应
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.selectedAnswer != -1) {
radioButtons[q.selectedAnswer].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 dialogInterface, int i) {
ExamActivity.this.finish();
}
})
.setNegativeButton("取消",null)
.show();
}
else{
//当前题目为最后一题时,告知用户作答正确的数量和作答错误的数量,并询问用户是否要查看错题
final List
wrongList=checkAnswer(list);
//作对所有题目
if(wrongList.size()==0){
new AlertDialog.Builder(ExamActivity.this)
.setTitle("提示")
.setMessage("恭喜你全部回答正确!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ExamActivity.this.finish();
}
}).show();
}
else
new AlertDialog.Builder(ExamActivity.this)
.setTitle("提示")
.setMessage("您答对了"+(list.size()-wrongList.size())+
"道题目;答错了"+wrongList.size()+"道题目。是否查看错题?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
//判断进入错题模式
wrongMode=true;
List newList=new ArrayList();
//将错误题目复制到newList中
for(int i=0;i< wrongList.size();i++){
newList.add(list.get(wrongList.get(i)));
}
//将原来的list清空
list.clear();
//将错误题目添加到原来的list中
for(int i=0;i }).show();
}
}
});
btn_previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (current > 0)//若当前题目不为第一题,点击previous按钮跳转到上一题;否则不响应
{
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.selectedAnswer != -1) {
radioButtons[q.selectedAnswer].setChecked(true);
}
}
}
});
//选择选项时更新选择
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
for (int i = 0; i < 4; i++) {
if (radioButtons[i].isChecked() == true) {
list.get(current).selectedAnswer = i;
break;
}
}
}
});
}
/*
判断用户作答是否正确,并将作答错误题目的下标生成list,返回给调用者
*/
private List
checkAnswer(List list) {
List wrongList = new ArrayList();
for(int i=0;i setContentView(R.layout.activity_main);
String DB_PATH = "/data/data/com.example.lenovo.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();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
Button btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ExamActivity.class);
startActivity(intent);
}
});
}
}
运行截图