Skip to content

Instantly share code, notes, and snippets.

@codinginflow
Created December 2, 2021 12:07
Show Gist options
  • Save codinginflow/bc98479f932c247dc0ecd62dd2e5367e to your computer and use it in GitHub Desktop.
Save codinginflow/bc98479f932c247dc0ecd62dd2e5367e to your computer and use it in GitHub Desktop.
Quiz App with SQLite Tutorial Part 12
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:padding="16dp"
tools:context="com.codinginflow.myawesomequiz.QuizActivity">
<TextView
android:id="@+id/text_view_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="Score: 0"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_view_question_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_score"
android:freezesText="true"
android:text="Question: 1/x"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_view_difficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_question_count"
android:freezesText="true"
android:text="Difficulty: "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_view_countdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:freezesText="true"
android:text="00:30"
android:textColor="@android:color/black"
android:textSize="40sp" />
<TextView
android:id="@+id/text_view_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/radio_group"
android:layout_marginBottom="16dp"
android:freezesText="true"
android:text="Here will be the question text\nHere will be the question text\nHere will be the question text"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="20sp" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<RadioButton
android:id="@+id/radio_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="Option 1" />
<RadioButton
android:id="@+id/radio_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="Option 2" />
<RadioButton
android:id="@+id/radio_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="Option 3" />
</RadioGroup>
<Button
android:id="@+id/button_confirm_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/radio_group"
android:layout_marginTop="16dp"
android:freezesText="true"
android:text="Confirm" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:padding="16dp"
tools:context="com.codinginflow.myawesomequiz.StartingScreenActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="My Awesome Quiz"
android:textColor="@android:color/black"
android:textSize="35sp" />
<TextView
android:id="@+id/text_view_highscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button_start_quiz"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:text="Highscore: 0"
android:textSize="20sp" />
<Button
android:id="@+id/button_start_quiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Start Quiz" />
<Spinner
android:id="@+id/spinner_difficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/button_start_quiz"
android:layout_below="@+id/button_start_quiz"
android:layout_marginTop="16dp" />
</RelativeLayout>
package com.codinginflow.myawesomequiz;
public class Category {
public static final int PROGRAMMING = 1;
public static final int GEOGRAPHY = 2;
public static final int MATH = 3;
private int id;
private String name;
public Category() {
}
public Category(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#795548</color>
<color name="colorPrimaryDark">#5D4037</color>
<color name="colorAccent">#FFEB3B</color>
<color name="colorBackground">#BCAAA4</color>
</resources>
package com.codinginflow.myawesomequiz;
import android.os.Parcel;
import android.os.Parcelable;
public class Question implements Parcelable {
public static final String DIFFICULTY_EASY = "Easy";
public static final String DIFFICULTY_MEDIUM = "Medium";
public static final String DIFFICULTY_HARD = "Hard";
private int id;
private String question;
private String option1;
private String option2;
private String option3;
private int answerNr;
private String difficulty;
private int categoryID;
public Question() {
}
public Question(String question, String option1, String option2, String option3,
int answerNr, String difficulty, int categoryID) {
this.question = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.answerNr = answerNr;
this.difficulty = difficulty;
this.categoryID = categoryID;
}
protected Question(Parcel in) {
id = in.readInt();
question = in.readString();
option1 = in.readString();
option2 = in.readString();
option3 = in.readString();
answerNr = in.readInt();
difficulty = in.readString();
categoryID = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(question);
dest.writeString(option1);
dest.writeString(option2);
dest.writeString(option3);
dest.writeInt(answerNr);
dest.writeString(difficulty);
dest.writeInt(categoryID);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Question> CREATOR = new Creator<Question>() {
@Override
public Question createFromParcel(Parcel in) {
return new Question(in);
}
@Override
public Question[] newArray(int size) {
return new Question[size];
}
};
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOption1() {
return option1;
}
public void setOption1(String option1) {
this.option1 = option1;
}
public String getOption2() {
return option2;
}
public void setOption2(String option2) {
this.option2 = option2;
}
public String getOption3() {
return option3;
}
public void setOption3(String option3) {
this.option3 = option3;
}
public int getAnswerNr() {
return answerNr;
}
public void setAnswerNr(int answerNr) {
this.answerNr = answerNr;
}
public String getDifficulty() {
return difficulty;
}
public void setDifficulty(String difficulty) {
this.difficulty = difficulty;
}
public int getCategoryID() {
return categoryID;
}
public void setCategoryID(int categoryID) {
this.categoryID = categoryID;
}
public static String[] getAllDifficultyLevels() {
return new String[]{
DIFFICULTY_EASY,
DIFFICULTY_MEDIUM,
DIFFICULTY_HARD
};
}
}
package com.codinginflow.myawesomequiz;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
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 android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
public class QuizActivity extends AppCompatActivity {
public static final String EXTRA_SCORE = "extraScore";
private static final long COUNTDOWN_IN_MILLIS = 30000;
private static final String KEY_SCORE = "keyScore";
private static final String KEY_QUESTION_COUNT = "keyQuestionCount";
private static final String KEY_MILLIS_LEFT = "keyMillisLeft";
private static final String KEY_ANSWERED = "keyAnswered";
private static final String KEY_QUESTION_LIST = "keyQuestionList";
private TextView textViewQuestion;
private TextView textViewScore;
private TextView textViewQuestionCount;
private TextView textViewDifficulty;
private TextView textViewCountDown;
private RadioGroup rbGroup;
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
private Button buttonConfirmNext;
private ColorStateList textColorDefaultRb;
private ColorStateList textColorDefaultCd;
private CountDownTimer countDownTimer;
private long timeLeftInMillis;
private ArrayList<Question> questionList;
private int questionCounter;
private int questionCountTotal;
private Question currentQuestion;
private int score;
private boolean answered;
private long backPressedTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
textViewQuestion = findViewById(R.id.text_view_question);
textViewScore = findViewById(R.id.text_view_score);
textViewQuestionCount = findViewById(R.id.text_view_question_count);
textViewDifficulty = findViewById(R.id.text_view_difficulty);
textViewCountDown = findViewById(R.id.text_view_countdown);
rbGroup = findViewById(R.id.radio_group);
rb1 = findViewById(R.id.radio_button1);
rb2 = findViewById(R.id.radio_button2);
rb3 = findViewById(R.id.radio_button3);
buttonConfirmNext = findViewById(R.id.button_confirm_next);
textColorDefaultRb = rb1.getTextColors();
textColorDefaultCd = textViewCountDown.getTextColors();
Intent intent = getIntent();
String difficulty = intent.getStringExtra(StartingScreenActivity.EXTRA_DIFFICULTY);
textViewDifficulty.setText("Difficulty: " + difficulty);
if (savedInstanceState == null) {
QuizDbHelper dbHelper = new QuizDbHelper(this);
questionList = dbHelper.getQuestions(difficulty);
questionCountTotal = questionList.size();
Collections.shuffle(questionList);
showNextQuestion();
} else {
questionList = savedInstanceState.getParcelableArrayList(KEY_QUESTION_LIST);
questionCountTotal = questionList.size();
questionCounter = savedInstanceState.getInt(KEY_QUESTION_COUNT);
currentQuestion = questionList.get(questionCounter - 1);
score = savedInstanceState.getInt(KEY_SCORE);
timeLeftInMillis = savedInstanceState.getLong(KEY_MILLIS_LEFT);
answered = savedInstanceState.getBoolean(KEY_ANSWERED);
if (!answered) {
startCountDown();
} else {
updateCountDownText();
showSolution();
}
}
buttonConfirmNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!answered) {
if (rb1.isChecked() || rb2.isChecked() || rb3.isChecked()) {
checkAnswer();
} else {
Toast.makeText(QuizActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show();
}
} else {
showNextQuestion();
}
}
});
}
private void showNextQuestion() {
rb1.setTextColor(textColorDefaultRb);
rb2.setTextColor(textColorDefaultRb);
rb3.setTextColor(textColorDefaultRb);
rbGroup.clearCheck();
if (questionCounter < questionCountTotal) {
currentQuestion = questionList.get(questionCounter);
textViewQuestion.setText(currentQuestion.getQuestion());
rb1.setText(currentQuestion.getOption1());
rb2.setText(currentQuestion.getOption2());
rb3.setText(currentQuestion.getOption3());
questionCounter++;
textViewQuestionCount.setText("Question: " + questionCounter + "/" + questionCountTotal);
answered = false;
buttonConfirmNext.setText("Confirm");
timeLeftInMillis = COUNTDOWN_IN_MILLIS;
startCountDown();
} else {
finishQuiz();
}
}
private void startCountDown() {
countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
@Override
public void onFinish() {
timeLeftInMillis = 0;
updateCountDownText();
checkAnswer();
}
}.start();
}
private void updateCountDownText() {
int minutes = (int) (timeLeftInMillis / 1000) / 60;
int seconds = (int) (timeLeftInMillis / 1000) % 60;
String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
textViewCountDown.setText(timeFormatted);
if (timeLeftInMillis < 10000) {
textViewCountDown.setTextColor(Color.RED);
} else {
textViewCountDown.setTextColor(textColorDefaultCd);
}
}
private void checkAnswer() {
answered = true;
countDownTimer.cancel();
RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
int answerNr = rbGroup.indexOfChild(rbSelected) + 1;
if (answerNr == currentQuestion.getAnswerNr()) {
score++;
textViewScore.setText("Score: " + score);
}
showSolution();
}
private void showSolution() {
rb1.setTextColor(Color.RED);
rb2.setTextColor(Color.RED);
rb3.setTextColor(Color.RED);
switch (currentQuestion.getAnswerNr()) {
case 1:
rb1.setTextColor(Color.GREEN);
textViewQuestion.setText("Answer 1 is correct");
break;
case 2:
rb2.setTextColor(Color.GREEN);
textViewQuestion.setText("Answer 2 is correct");
break;
case 3:
rb3.setTextColor(Color.GREEN);
textViewQuestion.setText("Answer 3 is correct");
break;
}
if (questionCounter < questionCountTotal) {
buttonConfirmNext.setText("Next");
} else {
buttonConfirmNext.setText("Finish");
}
}
private void finishQuiz() {
Intent resultIntent = new Intent();
resultIntent.putExtra(EXTRA_SCORE, score);
setResult(RESULT_OK, resultIntent);
finish();
}
@Override
public void onBackPressed() {
if (backPressedTime + 2000 > System.currentTimeMillis()) {
finishQuiz();
} else {
Toast.makeText(this, "Press back again to finish", Toast.LENGTH_SHORT).show();
}
backPressedTime = System.currentTimeMillis();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (countDownTimer != null) {
countDownTimer.cancel();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_SCORE, score);
outState.putInt(KEY_QUESTION_COUNT, questionCounter);
outState.putLong(KEY_MILLIS_LEFT, timeLeftInMillis);
outState.putBoolean(KEY_ANSWERED, answered);
outState.putParcelableArrayList(KEY_QUESTION_LIST, questionList);
}
}
package com.codinginflow.myawesomequiz;
import android.provider.BaseColumns;
public final class QuizContract {
private QuizContract() {
}
public static class CategoriesTable implements BaseColumns {
public static final String TABLE_NAME = "quiz_categories";
public static final String COLUMN_NAME = "name";
}
public static class QuestionsTable implements BaseColumns {
public static final String TABLE_NAME = "quiz_questions";
public static final String COLUMN_QUESTION = "question";
public static final String COLUMN_OPTION1 = "option1";
public static final String COLUMN_OPTION2 = "option2";
public static final String COLUMN_OPTION3 = "option3";
public static final String COLUMN_ANSWER_NR = "answer_nr";
public static final String COLUMN_DIFFICULTY = "difficulty";
public static final String COLUMN_CATEGORY_ID = "category_id";
}
}
package com.codinginflow.myawesomequiz;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.codinginflow.myawesomequiz.QuizContract.*;
import java.util.ArrayList;
import java.util.List;
public class QuizDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MyAwesomeQuiz.db";
private static final int DATABASE_VERSION = 1;
private static QuizDbHelper instance;
private SQLiteDatabase db;
private QuizDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static synchronized QuizDbHelper getInstance(Context context) {
if (instance == null) {
instance = new QuizDbHelper(context.getApplicationContext());
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
final String SQL_CREATE_CATEGORIES_TABLE = "CREATE TABLE " +
CategoriesTable.TABLE_NAME + "( " +
CategoriesTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
CategoriesTable.COLUMN_NAME + " TEXT " +
")";
final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
QuestionsTable.TABLE_NAME + " ( " +
QuestionsTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
QuestionsTable.COLUMN_QUESTION + " TEXT, " +
QuestionsTable.COLUMN_OPTION1 + " TEXT, " +
QuestionsTable.COLUMN_OPTION2 + " TEXT, " +
QuestionsTable.COLUMN_OPTION3 + " TEXT, " +
QuestionsTable.COLUMN_ANSWER_NR + " INTEGER, " +
QuestionsTable.COLUMN_DIFFICULTY + " TEXT, " +
QuestionsTable.COLUMN_CATEGORY_ID + " INTEGER, " +
"FOREIGN KEY(" + QuestionsTable.COLUMN_CATEGORY_ID + ") REFERENCES " +
CategoriesTable.TABLE_NAME + "(" + CategoriesTable._ID + ")" + "ON DELETE CASCADE" +
")";
db.execSQL(SQL_CREATE_CATEGORIES_TABLE);
db.execSQL(SQL_CREATE_QUESTIONS_TABLE);
fillCategoriesTable();
fillQuestionsTable();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CategoriesTable.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + QuestionsTable.TABLE_NAME);
onCreate(db);
}
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
private void fillCategoriesTable() {
Category c1 = new Category("Programming");
addCategory(c1);
Category c2 = new Category("Geography");
addCategory(c2);
Category c3 = new Category("Math");
addCategory(c3);
}
private void addCategory(Category category) {
ContentValues cv = new ContentValues();
cv.put(CategoriesTable.COLUMN_NAME, category.getName());
db.insert(CategoriesTable.TABLE_NAME, null, cv);
}
private void fillQuestionsTable() {
Question q1 = new Question("Programming, Easy: A is correct",
"A", "B", "C", 1,
Question.DIFFICULTY_EASY, Category.PROGRAMMING);
addQuestion(q1);
Question q2 = new Question("Geography, Medium: B is correct",
"A", "B", "C", 2,
Question.DIFFICULTY_MEDIUM, Category.GEOGRAPHY);
addQuestion(q2);
Question q3 = new Question("Math, Hard: C is correct",
"A", "B", "C", 3,
Question.DIFFICULTY_HARD, Category.MATH);
addQuestion(q3);
Question q4 = new Question("Math, Easy: A is correct",
"A", "B", "C", 1,
Question.DIFFICULTY_EASY, Category.MATH);
addQuestion(q4);
Question q5 = new Question("Non existing, Easy: A is correct",
"A", "B", "C", 1,
Question.DIFFICULTY_EASY, 4);
addQuestion(q5);
Question q6 = new Question("Non existing, Medium: B is correct",
"A", "B", "C", 2,
Question.DIFFICULTY_MEDIUM, 5);
addQuestion(q6);
}
private void addQuestion(Question question) {
ContentValues cv = new ContentValues();
cv.put(QuestionsTable.COLUMN_QUESTION, question.getQuestion());
cv.put(QuestionsTable.COLUMN_OPTION1, question.getOption1());
cv.put(QuestionsTable.COLUMN_OPTION2, question.getOption2());
cv.put(QuestionsTable.COLUMN_OPTION3, question.getOption3());
cv.put(QuestionsTable.COLUMN_ANSWER_NR, question.getAnswerNr());
cv.put(QuestionsTable.COLUMN_DIFFICULTY, question.getDifficulty());
cv.put(QuestionsTable.COLUMN_CATEGORY_ID, question.getCategoryID());
db.insert(QuestionsTable.TABLE_NAME, null, cv);
}
public List<Category> getAllCategories() {
List<Category> categoryList = new ArrayList<>();
db = getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + CategoriesTable.TABLE_NAME, null);
if (c.moveToFirst()) {
do {
Category category = new Category();
category.setId(c.getInt(c.getColumnIndex(CategoriesTable._ID)));
category.setName(c.getString(c.getColumnIndex(CategoriesTable.COLUMN_NAME)));
categoryList.add(category);
} while (c.moveToNext());
}
c.close();
return categoryList;
}
public ArrayList<Question> getAllQuestions() {
ArrayList<Question> questionList = new ArrayList<>();
db = getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + QuestionsTable.TABLE_NAME, null);
if (c.moveToFirst()) {
do {
Question question = new Question();
question.setId(c.getInt(c.getColumnIndex(QuestionsTable._ID)));
question.setQuestion(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_QUESTION)));
question.setOption1(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION1)));
question.setOption2(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION2)));
question.setOption3(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION3)));
question.setAnswerNr(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_ANSWER_NR)));
question.setDifficulty(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_DIFFICULTY)));
question.setCategoryID(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_CATEGORY_ID)));
questionList.add(question);
} while (c.moveToNext());
}
c.close();
return questionList;
}
public ArrayList<Question> getQuestions(int categoryID, String difficulty) {
ArrayList<Question> questionList = new ArrayList<>();
db = getReadableDatabase();
String selection = QuestionsTable.COLUMN_CATEGORY_ID + " = ? " +
" AND " + QuestionsTable.COLUMN_DIFFICULTY + " = ? ";
String[] selectionArgs = new String[]{String.valueOf(categoryID), difficulty};
Cursor c = db.query(
QuestionsTable.TABLE_NAME,
null,
selection,
selectionArgs,
null,
null,
null
);
if (c.moveToFirst()) {
do {
Question question = new Question();
question.setId(c.getInt(c.getColumnIndex(QuestionsTable._ID)));
question.setQuestion(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_QUESTION)));
question.setOption1(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION1)));
question.setOption2(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION2)));
question.setOption3(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION3)));
question.setAnswerNr(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_ANSWER_NR)));
question.setDifficulty(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_DIFFICULTY)));
question.setCategoryID(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_CATEGORY_ID)));
questionList.add(question);
} while (c.moveToNext());
}
c.close();
return questionList;
}
}
package com.codinginflow.myawesomequiz;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class StartingScreenActivity extends AppCompatActivity {
private static final int REQUEST_CODE_QUIZ = 1;
public static final String EXTRA_DIFFICULTY = "extraDifficulty";
public static final String SHARED_PREFS = "sharedPrefs";
public static final String KEY_HIGHSCORE = "keyHighscore";
private TextView textViewHighscore;
private Spinner spinnerDifficulty;
private int highscore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_screen);
textViewHighscore = findViewById(R.id.text_view_highscore);
spinnerDifficulty = findViewById(R.id.spinner_difficulty);
String[] difficultyLevels = Question.getAllDifficultyLevels();
ArrayAdapter<String> adapterDifficulty = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, difficultyLevels);
adapterDifficulty.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDifficulty.setAdapter(adapterDifficulty);
loadHighscore();
Button buttonStartQuiz = findViewById(R.id.button_start_quiz);
buttonStartQuiz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startQuiz();
}
});
}
private void startQuiz() {
String difficulty = spinnerDifficulty.getSelectedItem().toString();
Intent intent = new Intent(StartingScreenActivity.this, QuizActivity.class);
intent.putExtra(EXTRA_DIFFICULTY, difficulty);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_QUIZ) {
if (resultCode == RESULT_OK) {
int score = data.getIntExtra(QuizActivity.EXTRA_SCORE, 0);
if (score > highscore) {
updateHighscore(score);
}
}
}
}
private void loadHighscore() {
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
highscore = prefs.getInt(KEY_HIGHSCORE, 0);
textViewHighscore.setText("Highscore: " + highscore);
}
private void updateHighscore(int highscoreNew) {
highscore = highscoreNew;
textViewHighscore.setText("Highscore: " + highscore);
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(KEY_HIGHSCORE, highscore);
editor.apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment