Reloading/Refreshing activity 返回时(初学者)
Reloading/Refreshing activity when returning back (Beginner)
我正在制作一个测验应用程序只是为了体验,我有一个主要问题页面,上面写着问题编号、问题和选项...如果答案错误,它会等待 4 小时,如果下一个问题的正确时间是 24 小时.. 我正在从我使用 DatabaseHelper Class.The 链接的现有数据库中检索问题编号、问题、选项和答案,第一次它正确地从数据库中获取值。
我担心的是,当我从 24 小时等待 return 到主要问题页面时,它不会再次刷新文本视图,因此显示我保存在 xml 文件中的默认文本。
我知道当我使用 intent to return 到主页时,它继续 onRestart() 然后 onStart() 或 onResume() 而不是 onCreate() 但我尝试了我能找到的一切计算器和 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 小时后返回主 activity
https://i.stack.imgur.com/hVmrP.png
Activity 正确 class:
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 正在工作,但 activity 文本视图无论如何都成为默认值。
我试过 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);
}
更新:全局变量class:
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;
}
}
如果我理解你的问题,我想这就是你要添加的内容,因为当你从 activity:-
return 时,将调用 '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
内部移至 class 变量( 可能丢弃 onRestart):-
final Global globalVariable = (Global) getApplicationContext();
例如:-
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 .....
虽然创建一个获取数据并将其分配给视图的特定方法可能很有意义,但可以用一行从 onCreate
和 onResume
调用它。
2017 年 10 月 9 日编辑
我相信你遇到的问题是,正如我评论的那样,变量不是持久的。因此,我建议合并另一个 table 以获取问题进度。
这个新的 table 基本上是对相关问题的 link(下面的代码假设如果存在 linked 行,那么问题已经得到回答).它有 3 列 _id,问题的 link/reference(包含问题 _id)和状态列(未使用但可用于指示状态,例如已回答但不正确)。
当你保存存在问题的数据库时,我已经做到了,所以 MainActivity 总是尝试创建 progress table,所以它会添加table.
我没有合并调用另一个 activity。但是,mainActivity 中的 submitQuestion
方法是您执行此操作的地方,也是检查答案的地方。
首先是我使用的布局,即 activity_main.xml,所有这些包括标题的 TextView,问题的 TextView(将显示 抱歉,还没有准备好问题 当没有问题时)4 个带复选框的 TextView 用于答案(我已经这样做了,所以问题不需要 4 个选项,但 4 个是最大值)和最后一个提交按钮。
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 将问题 table 与进度 table 结合起来以确定下一个问题应该是什么。
我评论了查询解析为 SELECT * FROM questions LEFT JOIN progress ON questions._id = progress.questionref WHERE progress._id IS NULL ORDER BY questions.number ASC
This saying get all columns from the questions table which joined to the progress table 专栏当问题 _id 列与进度 table 的 questionref 列具有相同的值时,从进度 table 获取数据(如果不匹配,则所有进度列值将为空),因此 WHERE progress._id IS NULL(下一个问题是下一个尚未回答的问题) ORDER BY questions.number ASC 使得编号较低的问题出现在结果游标中编号较高的问题之前,我们从中得到第一行,然后提取问题 _id 列。请注意,我为列索引使用了硬编码的 0(我在使用名称时遇到问题,而且已经很晚了,所以只使用了 0)
你可能也注意到了table为进度table创建的不是在onCreate
方法中,而是在createProgresstable
方法这只允许它被独立调用(例如从 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());
是将创建进度 table 的行,因为如果数据库存在则不会调用 onCreate,请注意 CREATE TABLE progress IF NOT EXISTS 是已使用,因此如果进度 table 存在,它将保留。
这段代码只是为了添加一些问题所以我测试了:-
if (questions.getRowCount() < 1) {
addQuestions();
}
displayCurrentQuestion();
有什么不明白的地方欢迎提问
哦,这实际上是作为指南,因为它基于许多关于命名的假设,因此可能需要一段时间才能适应。您可能希望从基本复制代码开始,并在调整之前让它正常工作。
我正在制作一个测验应用程序只是为了体验,我有一个主要问题页面,上面写着问题编号、问题和选项...如果答案错误,它会等待 4 小时,如果下一个问题的正确时间是 24 小时.. 我正在从我使用 DatabaseHelper Class.The 链接的现有数据库中检索问题编号、问题、选项和答案,第一次它正确地从数据库中获取值。
我担心的是,当我从 24 小时等待 return 到主要问题页面时,它不会再次刷新文本视图,因此显示我保存在 xml 文件中的默认文本。
我知道当我使用 intent to return 到主页时,它继续 onRestart() 然后 onStart() 或 onResume() 而不是 onCreate() 但我尝试了我能找到的一切计算器和 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 小时后返回主 activity https://i.stack.imgur.com/hVmrP.png
Activity 正确 class:
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 正在工作,但 activity 文本视图无论如何都成为默认值。
我试过 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);
}
更新:全局变量class:
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;
}
}
如果我理解你的问题,我想这就是你要添加的内容,因为当你从 activity:-
return 时,将调用 '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
内部移至 class 变量( 可能丢弃 onRestart):-
final Global globalVariable = (Global) getApplicationContext();
例如:-
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 .....
虽然创建一个获取数据并将其分配给视图的特定方法可能很有意义,但可以用一行从 onCreate
和 onResume
调用它。
2017 年 10 月 9 日编辑
我相信你遇到的问题是,正如我评论的那样,变量不是持久的。因此,我建议合并另一个 table 以获取问题进度。
这个新的 table 基本上是对相关问题的 link(下面的代码假设如果存在 linked 行,那么问题已经得到回答).它有 3 列 _id,问题的 link/reference(包含问题 _id)和状态列(未使用但可用于指示状态,例如已回答但不正确)。
当你保存存在问题的数据库时,我已经做到了,所以 MainActivity 总是尝试创建 progress table,所以它会添加table.
我没有合并调用另一个 activity。但是,mainActivity 中的 submitQuestion
方法是您执行此操作的地方,也是检查答案的地方。
首先是我使用的布局,即 activity_main.xml,所有这些包括标题的 TextView,问题的 TextView(将显示 抱歉,还没有准备好问题 当没有问题时)4 个带复选框的 TextView 用于答案(我已经这样做了,所以问题不需要 4 个选项,但 4 个是最大值)和最后一个提交按钮。
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 将问题 table 与进度 table 结合起来以确定下一个问题应该是什么。
我评论了查询解析为 SELECT * FROM questions LEFT JOIN progress ON questions._id = progress.questionref WHERE progress._id IS NULL ORDER BY questions.number ASC
This saying get all columns from the questions table which joined to the progress table 专栏当问题 _id 列与进度 table 的 questionref 列具有相同的值时,从进度 table 获取数据(如果不匹配,则所有进度列值将为空),因此 WHERE progress._id IS NULL(下一个问题是下一个尚未回答的问题) ORDER BY questions.number ASC 使得编号较低的问题出现在结果游标中编号较高的问题之前,我们从中得到第一行,然后提取问题 _id 列。请注意,我为列索引使用了硬编码的 0(我在使用名称时遇到问题,而且已经很晚了,所以只使用了 0)
你可能也注意到了table为进度table创建的不是在onCreate
方法中,而是在createProgresstable
方法这只允许它被独立调用(例如从 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());
是将创建进度 table 的行,因为如果数据库存在则不会调用 onCreate,请注意 CREATE TABLE progress IF NOT EXISTS 是已使用,因此如果进度 table 存在,它将保留。
这段代码只是为了添加一些问题所以我测试了:-
if (questions.getRowCount() < 1) {
addQuestions();
}
displayCurrentQuestion();
有什么不明白的地方欢迎提问
哦,这实际上是作为指南,因为它基于许多关于命名的假设,因此可能需要一段时间才能适应。您可能希望从基本复制代码开始,并在调整之前让它正常工作。