返回时重新加载/刷新活动(初学者)

时间:2017-09-08 08:04:18

标签: java android mysql reload restart

我正在制作一个仅用于体验的测验应用程序,我有一个主要问题页面,上面写着问题编号,问题和选项...如果答案错误,它会给出4小时的等待时间,如果它是正确的接下来的问题是24小时..我正在从使用DatabaseHelper Class链接的现有数据库中检索问题编号,问题,选项和答案。第一次正确获取数据库中的值。

我担心的是,当我从24小时等待返回主要问题页面时,它不再刷新文本视图,因此向我显示我保存在xml文件中的默认文本。

我知道当我使用intent返回主页时,它继续onRestart()然后onStart()或onResume()而不是onCreate(),但我尝试了我在stackoverflow和youtube上找到的一切。

所以我特意用我的代码来形容它,因为它适用于其他人...... 请帮助我,如果有人可以,因为我说我尝试了很多东西,但也许我正确地实施它们。

主要问题页面代码和屏幕截图:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.database.Cursor;
import android.database.SQLException;

import java.io.IOException;

public class activityQuiz extends AppCompatActivity {

private RadioGroup radioGroup;
private RadioButton radioButton,option1,option2,option3,option4;
private Button submitBtn;
private String answer_text;
private TextView question_no,ques_text;
String table = "questions";
String[] columns = null;
String selection = "id =?";
String groupBy = null;
String having = null;
String orderBy = null;
Cursor c = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    final Global globalVariable = (Global) getApplicationContext();
    String[] selectionArgs = {String.valueOf(globalVariable.getQuestion_number())};
    radioGroup = (RadioGroup) findViewById(R.id.radio_q);
    submitBtn = (Button) findViewById(R.id.submit_btn);
    question_no = (TextView) findViewById(R.id.question_number);
    ques_text = (TextView) findViewById(R.id.tv3);
    option1 = (RadioButton) findViewById(R.id.radio1);
    option2 = (RadioButton) findViewById(R.id.radio2);
    option3 = (RadioButton) findViewById(R.id.radio3);
    option4 = (RadioButton) findViewById(R.id.radio4);
    submitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // get selected radio button from radioGroup
            int selectedId = radioGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(activityQuiz.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            String selected = radioButton.getText().toString();

            //if condition

            if(selected.equals(answer_text)) {
                finish();
                globalVariable.setQuestion_number(globalVariable.getQuestion_number()+ 1);
                Intent stud = new Intent(activityQuiz.this,activityCorrect.class);
                startActivity(stud);
                finish();
            }
            else
            {
                finish();
                Intent stud = new Intent(activityQuiz.this, activityWrong.class);
                startActivity(stud);
                finish();
            }
        }
    });
    database_paste(selectionArgs);
}
/*protected void onResume() {

    super.onResume();
    this.onCreate(null);
}*/

private void database_paste(String[] selectionArgs)
{

DatabaseHelper myDbHelper = new DatabaseHelper(activityQuiz.this);
try {
    myDbHelper.createDataBase();
} catch (IOException ioe) {
      throw new Error("Unable to create database");
}
try {
    myDbHelper.openDataBase();
} catch (SQLException sqle) {
    throw sqle;
}
Toast.makeText(activityQuiz.this, "Success", Toast.LENGTH_SHORT).show();
c = myDbHelper.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
if (c.moveToFirst()) {
    do {
        question_no.setText(c.getString(0));
        ques_text.setText(c.getString(1));
        option1.setText(c.getString(2));
        option2.setText(c.getString(3));
        option3.setText(c.getString(4));
        option4.setText(c.getString(5));
        answer_text = c.getString(6);
        Toast.makeText(activityQuiz.this,
                "_id: " + c.getString(0) + "\n"+
                "_answer" + c.getString(6),
                Toast.LENGTH_LONG).show();
    } while (c.moveToNext());


}
}


}

https://i.stack.imgur.com/8bwvE.png 然后我选择正确的答案,并在24小时后回到主要活动 https://i.stack.imgur.com/hVmrP.png

活动正确的课程:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import cn.iwgang.countdownview.CountdownView;
import cn.iwgang.countdownview.DynamicConfig;

public class activityCorrect extends AppCompatActivity {


CountdownView countview1;
TextView tv1 ;
Button nextQues;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_correct);
    countview1 = (CountdownView) findViewById(R.id.countdownView1);
    countview1.start(10000);
    tv1 = (TextView) findViewById(R.id.tv1);
    nextQues = (Button) findViewById(R.id.next_question);

    countview1.setOnCountdownEndListener(new CountdownView.OnCountdownEndListener()
    {
        @Override
        public void onEnd(CountdownView cv)
        {
            tv1.setText("Next Question is Ready for you");
            nextQues.setVisibility(View.VISIBLE);
        }
    });

    nextQues.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent stud = new Intent(activityCorrect.this,activityQuiz.class);
            startActivity(stud);
            finish();
        }
    });

}

}

更新:

我尝试使用onResume(),其中的toast正在运行,但活动textviews仍然是默认值。

我试过onRestart()但是用toast测试后,我发现onRestart()方法没有被执行

     @Override
    protected void onRestart() {
    super.onRestart();
    Toast.makeText(activityQuiz.this,
            "Restart is working",
            Toast.LENGTH_LONG).show();
    final Global globalVariable = (Global) getApplicationContext();
    String[] selectionArgs = {String.valueOf(globalVariable.getQuestion_number())};
    database_paste(selectionArgs);
    }

更新:GLOBAL变量类:

package com.example.nikko.mygift;

import android.app.Application;

public class Global extends Application{

private int main_screen_check = 0,question_number = 1;

public int getQuestion_number() {
    return question_number;             //Question index memory
}

public void setQuestion_number(int question_number) {
    this.question_number = question_number;
}



public int getMain_screen_check() {
    return main_screen_check;           //Main page check
}

public void setMain_screen_check(int main_screen_check) {
    this.main_screen_check = main_screen_check;

}

}

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,我认为这就是你想要添加的内容' onResume'从活动返回时将被调用: -

@Override
protected void onResume() {
    super.onResume();
    String[] selectionArgs = {String.valueOf(globalVariable.getQuestion_number())};
    c = myDbHelper.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    if (c.moveToFirst()) {
        do {
            question_no.setText(c.getString(0));
            ques_text.setText(c.getString(1));
            option1.setText(c.getString(2));
            option2.setText(c.getString(3));
            option3.setText(c.getString(4));
            option4.setText(c.getString(5));
            answer_text = c.getString(6);
            Toast.makeText(activityQuiz.this,
            "_id: " + c.getString(0) + "\n"+
            "_answer" + c.getString(6),
            Toast.LENGTH_LONG).show();
        } while (c.moveToNext());
    }
}

将其从onRestart内部移动为类变量(可能丢弃onRestart ): -

final Global globalVariable = (Global) getApplicationContext();

e.g。 : -

public class activityQuiz extends AppCompatActivity {

    private RadioGroup radioGroup;
    private RadioButton radioButton,option1,option2,option3,option4;
    private Button submitBtn;
    private String answer_text;
    private TextView question_no,ques_text;
    final Global globalVariable = (Global) getApplicationContext();
    ....... rest of the code .....

尽管创建一个获取数据并将其分配给视图的特定方法可能是有意义的,然后可以使用单行从onCreateonResume调用此数据。

编辑于10/09/2017

我相信你遇到的问题,就像我评论说变量不是持久的。因此,我建议在问题进展中加入另一个表格。

这个新表基本上只是一个相关问题的链接(下面的代码假定如果存在一个链接的行,那么问题已得到解答)。它有3列_id,问题的链接/引用(包含问题_id)和状态列(未使用但可用于表示状态,例如已回答但不正确)。

当您保存存在问题的数据库时,我已经做到这一点,以便MainActivity总是尝试创建进度表,因此它会添加表。

我没有参加另一项活动。但是,mainActivity中的 submitQuestion 方法是您执行此操作的方法,这也是检查答案的位置。

首先我使用的布局,即 activity_main.xml ,所有这些包括标题的TextView,问题的TextView(将显示抱歉没有问题准备好了当没有问题时)4带有CheckBoxes的TextView答案(我已经做到了这样,问题不需要4个选项,但4个是最大值),最后是SUBMIT按钮。< / p>

activity_main.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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="mjt.so46111824.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Questions" />

    <TextView
        android:id="@+id/question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/answer1_text"
            android:layout_width="0dp"
            android:layout_weight="10"
            android:layout_height="match_parent" />
        <CheckBox
            android:id="@+id/answer1_checkbox"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/answer2_text"
            android:layout_width="0dp"
            android:layout_weight="10"
            android:layout_height="match_parent" />
        <CheckBox
            android:id="@+id/answer2_checkbox"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/answer3_text"
            android:layout_width="0dp"
            android:layout_weight="10"
            android:layout_height="match_parent" />
        <CheckBox
            android:id="@+id/answer3_checkbox"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/answer4_text"
            android:layout_width="0dp"
            android:layout_weight="10"
            android:layout_height="match_parent" />
        <CheckBox
            android:id="@+id/answer4_checkbox"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </LinearLayout>
    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SUBMIT"/>
</LinearLayout>

DBHelper.java: -

class DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "questions";
    public static final String TABLENAME = "questions";
    public static final String IDCOLUMN = "_id";
    public static final String QUESTIONCOLUMN = "question";
    public static final String NUMBERCOLUMN = "number";
    public static final String ANSWER1COLUMN = "answer1";
    public static final String ANSWER2COLUMN = "answer2";
    public static final String ANSWER3COLUMN = "answer3";
    public static final String ANSWER4COLUMN = "answer4";
    public static final String CORRECTCOLUMN = "correctanswer";

    public static final String PROGRESSTABLENAME = "progress";
    public static final String QUESTIONREFCOLUMN = "questionref";
    public static final String QUESTIONSTATECOLUMN = "questionstate";


    String crtsql;

    DBHelper(Context context) {
        super(context, DBNAME, null, 1);
        this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

            crtsql = "CREATE TABLE IF NOT EXISTS " + TABLENAME + "(" +
                    IDCOLUMN + " INTEGER PRIMARY KEY, " +
                    QUESTIONCOLUMN + " TEXT, " +
                    NUMBERCOLUMN + " INTEGER, " +
                    ANSWER1COLUMN + " TEXT, " +
                    ANSWER2COLUMN + " TEXT, " +
                    ANSWER3COLUMN + " TEXT, " +
                    ANSWER4COLUMN + " TEXT, " +
                    CORRECTCOLUMN + " INTEGER " +
                    ")";
        db.execSQL(crtsql);
        createProgresstable(db);
    }
    public void createProgresstable(SQLiteDatabase db) {

        String crtsql = "CREATE TABLE IF NOT EXISTS " + PROGRESSTABLENAME + "(" +
                IDCOLUMN + " INTEGER PRIMARY KEY, " +
                QUESTIONREFCOLUMN + " INTEGER NOT NULL," +
                QUESTIONSTATECOLUMN + "INTEGER DEFAULT 0" +
                ")";
        db.execSQL(crtsql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public void insertQuestion(String question,
                               int number,
                               String answer1,
                               String answer2,
                               String answer3,
                               String answer4,
                               int correctanswer) {

        if (correctanswer < 1 || correctanswer > 4) {
            Log.d("QUESTIONS","Invalid Question - Correct Answer outside of range 1-4.");
            return;
        }
        ContentValues cv = new ContentValues();
        cv.put(QUESTIONCOLUMN,question);
        cv.put(NUMBERCOLUMN,number);
        cv.put(ANSWER1COLUMN,answer1);
        cv.put(ANSWER2COLUMN,answer2);
        cv.put(ANSWER3COLUMN,answer3);
        cv.put(ANSWER4COLUMN,answer4);
        cv.put(CORRECTCOLUMN,correctanswer);
        this.getWritableDatabase().insert(TABLENAME,null,cv);

    }

    public int getRowCount() {
        Cursor csr = this.getWritableDatabase().query(TABLENAME,null,null,null,null,null,null);
        int rv = csr.getCount();
        csr.close();
        return rv;
    }

    public Cursor getQuestionRow(long questionid) {
        String whereclause = IDCOLUMN + "=?";
        String[] whereargs = {Long.toString(questionid)};
        return this.getWritableDatabase().query(TABLENAME,
                null,
                whereclause,whereargs,
                null,null,null);
    }

    public void insertProgress(Long questionid) {
        ContentValues cv = new ContentValues();
        cv.put(QUESTIONREFCOLUMN,questionid);
        this.getWritableDatabase().insert(PROGRESSTABLENAME,null,cv);
    }

    public Cursor getProgressOfQuestion(long questionid) {
        String whereclause = QUESTIONREFCOLUMN + "=?";
        String[] whereargs = {Long.toString(questionid)};
        return this.getWritableDatabase().query(PROGRESSTABLENAME,null,whereclause,whereargs,null,null,null
        );
    }

    public void updateProgressStatus(long questionid,int newstatus) {
        String whereclause = QUESTIONREFCOLUMN + "=?";
        String[] wherargs = {Long.toString(questionid)};
        ContentValues cv = new ContentValues();
        cv.put(QUESTIONSTATECOLUMN,newstatus);
        this.getWritableDatabase().update(PROGRESSTABLENAME,
                cv,
                whereclause,
                wherargs
        );
    }

    public long getNextQuestionID() {

        //SELECT * FROM questions LEFT JOIN progress ON questions._id = progress.questionref WHERE progress._id IS NULL  ORDER BY questions.number ASC

        long rv = 0;
        SQLiteDatabase db = this.getWritableDatabase();
        String whereclause = PROGRESSTABLENAME + "." + IDCOLUMN + " IS NULL ";
        String[] whereargs = null;

        Cursor csr = db.query(TABLENAME +
                        " LEFT JOIN " + PROGRESSTABLENAME +
                        " ON " + TABLENAME + "." + IDCOLUMN + " = " +
                        PROGRESSTABLENAME + "." + QUESTIONREFCOLUMN,
                null,
                whereclause,
                whereargs,
                null,null,
                TABLENAME + "." + NUMBERCOLUMN + " ASC");
        if (csr.moveToFirst()) {
            rv = csr.getLong(0);
        }
        csr.close();
        return rv;
    }
}

唯一令人生畏的方法应该是 getNextQuestionID ,因为它使用JOIN将问题表与进度表结合起来,以确定下一个问题应该是什么。

我已评论查询解析的内容为SELECT * FROM questions LEFT JOIN progress ON questions._id = progress.questionref WHERE progress._id IS NULL ORDER BY questions.number ASC

当问题_id列具有相同的值时,此说明会从问题表中获取与进度表列数据相关联的所有列,这些列是从进度表中获取的作为进度表的questionref列(如果没有匹配则所有进度列值将为null),因此WHERE progress._id IS NULL(下一个问题是未回答的下一个问题)ORDER BY questions.number ASC使得较低编号的问题出现在结果光标中较高编号的问题之前,从中我们得到第一行,然后提取问题_id列。注意我已经使用了硬编码的0作为列索引(我在使用名称时遇到了问题,而且它很晚才使用0)

您可能还注意到,为进度表创建的表不在onCreate方法中,但在 createProgresstable 方法中,这只允许它被独立调用(例如来自MainActivity)。

MainActivity

public class MainActivity extends AppCompatActivity {

    DBHelper questions;

    TextView mQuestion;
    TextView mAnswer1;
    TextView mAnswer2;
    TextView mAnswer3;
    TextView mAnswer4;
    CheckBox mCheckBox1;
    CheckBox mCheckBox2;
    CheckBox mCheckBox3;
    CheckBox mCheckBox4;
    Button mSubmit;

    long mCurrentQuestion = 0;
    int mCorrectAnswer = -1;
    int mCurrentAnswer = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mQuestion = (TextView) findViewById(R.id.question);
        mAnswer1 = (TextView) findViewById(R.id.answer1_text);
        mAnswer2 = (TextView) findViewById(R.id.answer2_text);
        mAnswer3 = (TextView) findViewById(R.id.answer3_text);
        mAnswer4 = (TextView) findViewById(R.id.answer4_text);
        mCheckBox1 = (CheckBox) findViewById(R.id.answer1_checkbox);
        mCheckBox2 = (CheckBox) findViewById(R.id.answer2_checkbox);
        mCheckBox3 = (CheckBox) findViewById(R.id.answer3_checkbox);
        mCheckBox4 = (CheckBox) findViewById(R.id.answer4_checkbox);
        mSubmit = (Button) findViewById(R.id.submit);
        mSubmit.setVisibility(View.INVISIBLE);
        setCheckBoxListeners();
        setSubmitButtonListener();

        questions = new DBHelper(this);         //Get Questions DBHelper
        questions.createProgresstable(questions.getWritableDatabase());

        if (questions.getRowCount() < 1) {
            addQuestions();
        }
        displayCurrentQuestion();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCurrentQuestion = questions.getNextQuestionID();

    }

    void displayCurrentQuestion() {
        mQuestion.setText("Sorry no questions are ready as yet");
        mAnswer1.setVisibility(View.INVISIBLE);
        mAnswer2.setVisibility(View.INVISIBLE);
        mAnswer3.setVisibility(View.INVISIBLE);
        mAnswer4.setVisibility(View.INVISIBLE);
        mCheckBox1.setVisibility(View.INVISIBLE);
        mCheckBox2.setVisibility(View.INVISIBLE);
        mCheckBox3.setVisibility(View.INVISIBLE);
        mCheckBox4.setVisibility(View.INVISIBLE);
        mSubmit.setVisibility(View.INVISIBLE);
        Cursor csr = questions.getQuestionRow(mCurrentQuestion = questions.getNextQuestionID());
        if (csr.moveToFirst()) {
            mQuestion.setText(csr.getString(csr.getColumnIndex(DBHelper.QUESTIONCOLUMN)));
            String answer1 = csr.getString(csr.getColumnIndex(DBHelper.ANSWER1COLUMN));
            String answer2 = csr.getString(csr.getColumnIndex(DBHelper.ANSWER2COLUMN));
            String answer3 = csr.getString(csr.getColumnIndex(DBHelper.ANSWER3COLUMN));
            String answer4 = csr.getString(csr.getColumnIndex(DBHelper.ANSWER4COLUMN));
            mCorrectAnswer = csr.getInt(csr.getColumnIndex(DBHelper.CORRECTCOLUMN));
            if (answer1.length() > 0) {
                mAnswer1.setText(answer1);
                mAnswer1.setVisibility(View.VISIBLE);
                mCheckBox1.setVisibility(View.VISIBLE);
            }
            if (answer2.length() > 0) {
                mAnswer2.setText(answer2);
                mAnswer2.setVisibility(View.VISIBLE);
                mCheckBox2.setVisibility(View.VISIBLE);
            }
            if (answer3.length() > 0) {
                mAnswer3.setText(answer3);
                mAnswer3.setVisibility(View.VISIBLE);
                mCheckBox3.setVisibility(View.VISIBLE);
            }
            if (answer4.length() > 0) {
                mAnswer4.setText(answer4);
                mAnswer4.setVisibility(View.VISIBLE);
                mCheckBox4.setVisibility(View.VISIBLE);
            }
        }
        csr.close();
    }

    void submitQuestion() {
        String questionresult = "Incorrect.";
        if (mCurrentAnswer == mCorrectAnswer) {
            questionresult = " Correct.";
            questions.insertProgress(mCurrentQuestion);
        }
        Toast.makeText(this,
                "Submitted Question " +
                        Long.toString(mCurrentQuestion) +
                        " Your answer was " + questionresult
                ,
                Toast.LENGTH_SHORT
        ).show();
        displayCurrentQuestion();
    }

    void setSubmitButtonListener() {
        mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitQuestion();
            }
        });
    }

    void setCheckBoxListeners() {
        mCheckBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b) {
                    mCheckBox2.setChecked(false);
                    mCheckBox3.setChecked(false);
                    mCheckBox4.setChecked(false);
                    mSubmit.setVisibility(View.VISIBLE);
                    mCurrentAnswer = 1;
                }
            }
        });
        mCheckBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b) {
                    mCheckBox1.setChecked(false);
                    mCheckBox3.setChecked(false);
                    mCheckBox4.setChecked(false);
                    mSubmit.setVisibility(View.VISIBLE);
                    mCurrentAnswer = 2;
                }
            }
        });
        mCheckBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b) {
                    mCheckBox1.setChecked(false);
                    mCheckBox2.setChecked(false);
                    mCheckBox4.setChecked(false);
                    mSubmit.setVisibility(View.VISIBLE);
                    mCurrentAnswer = 3;
                }
            }
        });
        mCheckBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b) {
                    mCheckBox1.setChecked(false);
                    mCheckBox2.setChecked(false);
                    mCheckBox3.setChecked(false);
                    mSubmit.setVisibility(View.VISIBLE);
                    mCurrentAnswer = 4;
                }
            }
        });
    }

    void addQuestions() {
        questions.insertQuestion("What was the name that nobody should have known?",
                1,
                "The Man with no Name.",
                "Alan Parson Ah Ha.",
                "Engelbert Humperdink.",
                "Rumplestiltskin.",
                4
        );
        questions.insertQuestion("What Band released the Album entitled Crime of the Cetury",
                2,
                "The Tramps",
                "Super Tramp",
                "Trankenstiens Feast",
                "Tranvison Vamp",
                2
        );
        questions.insertQuestion("Where is the worlds only Treacle Mine?",
                3,
                "Ginge",
                "Ardington",
                "Lockinge",
                "Peasemoore",
                1
        );
    }
}

希望以上不会让人望而生畏,基本上还有很多重复。

questions.createProgresstable(questions.getWritableDatabase());是创建进度表的行,因为如果数据库存在,则不会调用onCreate,请注意,如果使用了进度表,则使用CREATE TABLE进程IF NOT EXISTS将继续。

此片段仅用于添加一些问题,因此我测试: -

    if (questions.getRowCount() < 1) {
        addQuestions();
    }
    displayCurrentQuestion();

如果您有任何不明白的事情,请随时提问。

哦,这真的是作为一个指南,因为基于很多关于命名的假设所以它可能需要一段时间才能适应。您可能希望从基本上复制代码开始,然后在适应之前使其工作。