If I understand you question I think this is what you want to add as 'onResume' will be called when you return from the activity:-
@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());
}
}
And move this from inside onRestart
to be a class variable (perhaps discard 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 .....
Although it would probably make sense to create a specific method that gets the data and assigns it to the Views, this could then be called from onCreate
and onResume
with a single line.
Edited 10/09/2017
I believe the issue you were having, is as I commented that the variable was not persistent. As such I'd suggest incorporating another table for question progress.
This new table would be relatively simply basically a link to the related question (the code below assumes that if a linked row exists, then the question has been answered). It has 3 columns _id, the link/reference to the question (contains the questions _id) and a status column (not used but could be used to indicate a state e.g. answered but incorrect).
As you save that the database with questions exists, I've made it so that the MainActivity always tries to create the progress table, so it would add the table.
I've not incoporated calling another activity. However, the submitQuestion
method in the mainActivity is where you'd do this, this is also where the answer is checked.
First the layout I used i.e. activity_main.xml, all this consists of is A TextView for the heading, A TextView for the Question (which will display Sorry there are no Questions ready yet when there are no questions left) 4 TextView with CheckBoxes for the answers (I've made it so that questions don't need 4 options but 4 is the max) and finally a SUBMIT button.
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;
}
}
The only daunting method should be getNextQuestionID
as this uses a JOIN to combine the questions table with the progress table for determining what the next question should be.
I've commented what the query resolves to which is 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 is joined to the progress table column data being obtained from the progress table when the questions _id column has the same value as the questionref column of the progress table (if no match then all the progress column values will be null), hence the WHERE progress._id IS NULL (the next question is the next question that has not been answered) ORDER BY questions.number ASC makes it so that lower numbered questions appear before higher numbered questions in the resultant cursor from which we get the first row and then extract the question _id column. Note I've used a hard coded 0 for the column index (I was having issues with using the name and it's pretty late so just used 0)
You may also notice that the table create for the progress table is not in the onCreate
method, but in the createProgresstable
method this just allows it to be called independently (e.g. from the 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
);
}
}
Hopefully the above won't be to daunting, basically there's a lot of repetition.
questions.createProgresstable(questions.getWritableDatabase());
is the line where the progress table will be created, as onCreate won't be called if the database exists, note that CREATE TABLE progress IF NOT EXISTS is used so if the progress table exists it will remain.
This snippet is just for adding some questions so I test :-
if (questions.getRowCount() < 1) {
addQuestions();
}
displayCurrentQuestion();
Please feel free to ask questions if there's anything you don't understand.
Oh and this is really intended as a guide as is based on quite a few assumptions regarding naming so it will likely take a while to adapt. You may wish to start by basically copying the code and getting that working before adapting it.