Android 题目动态存储+倒计时功能实现

发布于:2022-11-09 ⋅ 阅读:(15) ⋅ 点赞:(0) ⋅ 评论:(0)

点击获取每日答题完整布局
从每日答题代码做出以下修改

一、数据库存储题目信息

题目信息数据库存储,并从数据库中读取题目信息

1、案例演示

将题目信息存储到android自带数据库中
在这里插入图片描述
从中随机取5道题,从运行结果可以看出题目类型是随机的
请添加图片描述

2、实现步骤

2.1、MyDataBaseHepler.java

该类用于创建数据表questions,表结构如下

public class MyDataBaseHepler extends SQLiteOpenHelper {
    public static final String CREATE_QUESTIONS="create table questions(" +
            "id integer primary key autoincrement," +
            "questionstem text," +
            "optionA text," +
            "optionB text," +
            "optionC text," +
            "optionD text," +
            "correctOptions text," +
            "type integer)";
    private Context text;

    public MyDataBaseHepler(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        text=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_QUESTIONS);
        Toast.makeText(text, "创建数据库成功", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

2.2、QuestionsDao.java

数据库操作类用于数据的插入和查询,加查询结果用游标遍历出来存到集合中

public class QuestionsDao {
    private MyDataBaseHepler hepler;
    public QuestionsDao(Context context){
        hepler=new MyDataBaseHepler(context,"question",null,1);
    }
    //添加数据
    public long insertQuestion(Question question){
        SQLiteDatabase database=hepler.getWritableDatabase();
        ContentValues values=new ContentValues();
        String options[] =question.getOptions();
        values.put("questionStem",question.getQuestionStem());
        values.put("optionA",options[0]);
        values.put("optionB",options[1]);
        values.put("optionC",options[2]);
        values.put("optionD",options[3]);

        values.put("correctOptions",question.getCorrectOptions());
        values.put("type",question.getQuestionTypes());
        long i=database.insert("questions",null,values);
        database.close();
        return i;
    }
    //随机查询五道题
    public ArrayList queryFive(){
        SQLiteDatabase database=hepler.getWritableDatabase();
        ArrayList list=new ArrayList();
        String sql="select * from questions order by random() limit 5";
        Cursor cursor=database.rawQuery(sql,null);
        list=convertCursor(cursor);
        return list;
    }
    public ArrayList convertCursor(Cursor cursor){
        ArrayList list=new ArrayList();
        if (cursor!=null&&cursor.moveToFirst()){
            do {
                Question question=new Question();
                question.setQuestionStem(cursor.getString(1));
                String options[]=new String[]{
                cursor.getString(cursor.getColumnIndex("optionA")),
                cursor.getString(cursor.getColumnIndex("optionB")),
                cursor.getString(cursor.getColumnIndex("optionC")),
                cursor.getString(cursor.getColumnIndex("optionD"))};
                question.setOptions(options);
                question.setCorrectOptions(cursor.getString(cursor.getColumnIndex("correctOptions")));
                question.setQuestionTypes(cursor.getInt(cursor.getColumnIndex("type")));
                list.add(question);
            }while (cursor.moveToNext());
        }
        return list;
    }
}

2.3、QuestionActivity.java

改写initQuestions()方法,为了区分是否是第一次调用,我们用SharedPreferences存储一个布尔类型的变量开始该变量值为true,当第一次调用结束,即数据插入一次后,该变量为false,第二次调用直接进行查询调用数据

  • 这里我们插入了7条题目
  • 存储布尔类型的变量is
  • 统一选项个数为4个
    public void initQuestions(){
        Log.e("信息更新", "执行了");
        QuestionsDao dao=new QuestionsDao(this);
        //读取is值
        SharedPreferences spf=getPreferences(MODE_PRIVATE);
        boolean is=spf.getBoolean("is",true);
        if(is){
            SharedPreferences.Editor editor=spf.edit();
            editor.putBoolean("is",false);
            editor.commit();
            Question question=new Question();
            //题干
            question.setQuestionStem("1、哪个季节天气最热?");
            //选项
            question.setOptions(new String[]{"春","夏","秋","冬"});
            //正确的答案
            question.setCorrectOptions("1");
            //题目类型
            question.setQuestionTypes(1);
            //把题目添加到集合中
            questions.add(question);

            Question question2=new Question();
            //题干
            question2.setQuestionStem("2、坤坤喜欢的运动是什么?");
            //选项
            question2.setOptions(new String[]{"唱","跳","rap","篮球"});
            //正确的答案
            question2.setCorrectOptions("1,2");
            //题目类型
            question2.setQuestionTypes(2);
            //把题目添加到集合中
            questions.add(question2);

            Question question3=new Question();
            //题干
            question3.setQuestionStem("3、属于前端的是?");
            //选项
            question3.setOptions(new String[]{"html","css","java","mysql"});
            //正确的答案
            question3.setCorrectOptions("0,1");
            //题目类型
            question3.setQuestionTypes(2);
            //把题目添加到集合中
            questions.add(question3);

            Question question4=new Question();
            //题干
            question4.setQuestionStem("4、属于后端的是?");
            //选项
            question4.setOptions(new String[]{"java","c","ppt","world"});
            //正确的答案
            question4.setCorrectOptions("0,1");
            //题目类型
            question4.setQuestionTypes(2);
            //把题目添加到集合中
            questions.add(question4);

            Question question5=new Question();
            //题干
            question5.setQuestionStem("5、我使用的手机品牌是?");
            //选项
            question5.setOptions(new String[]{"苹果","化为","OPPO","VIVO"});
            //正确的答案
            question5.setCorrectOptions("3");
            //题目类型
            question5.setQuestionTypes(1);
            //把题目添加到集合中
            questions.add(question5);

            Question question6=new Question();
            //题干
            question6.setQuestionStem("6、草帽海贼团?");
            //选项
            question6.setOptions(new String[]{"路飞","娜美","山治","索隆"});
            //正确的答案
            question6.setCorrectOptions("0,1,2,3");
            //题目类型
            question6.setQuestionTypes(2);
            //把题目添加到集合中
            questions.add(question6);

            Question question7=new Question();
            //题干
            question7.setQuestionStem("7、你喜欢的单词?");
            //选项
            question7.setOptions(new String[]{"apple","food","ate","good"});
            //正确的答案
            question7.setCorrectOptions("3");
            //题目类型
            question7.setQuestionTypes(1);
            //把题目添加到集合中
            questions.add(question7);

            dao.insertQuestion(question);
            dao.insertQuestion(question2);
            dao.insertQuestion(question3);
            dao.insertQuestion(question4);
            dao.insertQuestion(question5);
            dao.insertQuestion(question6);
            dao.insertQuestion(question7);
        }
        questions=dao.queryFive();

    }

二、倒计时功能实现

1、案例演示

时间结束返回答题结束窗口
请添加图片描述

2、实现步骤

2.1、QuestionsDao.java

 public class QuestionActivity extends AppCompatActivity {
    private TextView time;
    private int myTime=2*60;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    showDialog();
                    break;
                case 1:
                    int minute=msg.arg1;
                    int second=msg.arg2;
                    time.setText("剩余时间"+minute+":"+second);
                    break;
                default:
            }
        }
    };
    private class ThreadRunnable implements Runnable{
        @Override
        public void run() {
            myTime--;
            if (myTime==0){
                Message message=new Message();
                message.what=0;//代表倒计时结束
                handler.sendMessage(message);//发送消息
            }else {
                Message message=new Message();
                message.what=1;
                message.arg1=myTime/60;
                message.arg2=myTime%60;
                handler.sendMessage(message);
                handler.postDelayed(this,1000);
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        
        ThreadRunnable runnable=new ThreadRunnable();
        runnable.run();
    }
  }