Quiz part completed 50%
This commit is contained in:
parent
ac219546ab
commit
7a2ef373b1
|
@ -0,0 +1,65 @@
|
|||
package com.example.alzapp;
|
||||
|
||||
public class Question {
|
||||
|
||||
private String question;
|
||||
private String option1;
|
||||
private String option2;
|
||||
private String option3;
|
||||
private int answerNr;
|
||||
|
||||
|
||||
public Question(){}
|
||||
|
||||
public Question(String question, String option1, String option2, String option3, int answerNr) {
|
||||
this.question = question;
|
||||
this.option1 = option1;
|
||||
this.option2 = option2;
|
||||
this.option3 = option3;
|
||||
this.answerNr = answerNr;
|
||||
}
|
||||
|
||||
|
||||
/** Getter and setter methods **/
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.example.alzapp;
|
||||
|
||||
import android.provider.BaseColumns;
|
||||
|
||||
public final class QuizContract {
|
||||
|
||||
private QuizContract(){}
|
||||
|
||||
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";
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
package com.example.alzapp;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
import com.example.alzapp.QuizContract.* ;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class QuizDbHelper extends SQLiteOpenHelper {
|
||||
|
||||
private static final String DATABASE_NAME = "Quiz.db";
|
||||
private static final int DATABASE_VERSION = 1;
|
||||
|
||||
private SQLiteDatabase db;
|
||||
|
||||
public QuizDbHelper(Context context) {
|
||||
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
|
||||
this.db = db;
|
||||
|
||||
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" +
|
||||
")";
|
||||
|
||||
db.execSQL(SQL_CREATE_QUESTIONS_TABLE);
|
||||
|
||||
fillQuestionsTable();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
db.execSQL("DROP TABLE IF EXISTS " + QuestionsTable.TABLE_NAME);
|
||||
onCreate(db);
|
||||
}
|
||||
|
||||
private void fillQuestionsTable(){
|
||||
Question q1 = new Question("A is correct", "A", "B", "C", 1);
|
||||
addQuestion(q1);
|
||||
|
||||
Question q2 = new Question("B is correct", "A", "B", "C", 2);
|
||||
addQuestion(q2);
|
||||
|
||||
Question q3 = new Question("C is correct", "A", "B", "C", 3);
|
||||
addQuestion(q3);
|
||||
|
||||
Question q4 = new Question("A is correct again", "A", "B", "C", 1);
|
||||
addQuestion(q4);
|
||||
|
||||
Question q5 = new Question("B is correct again", "A", "B", "C", 2);
|
||||
addQuestion(q5);
|
||||
|
||||
}
|
||||
|
||||
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_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());
|
||||
|
||||
db.insert(QuestionsTable.TABLE_NAME, null, cv);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public List<Question> getAllQuestions(){
|
||||
List<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.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)));
|
||||
} while (c.moveToNext());
|
||||
}
|
||||
|
||||
c.close();
|
||||
return questionList;
|
||||
}
|
||||
}
|
|
@ -1,15 +1,86 @@
|
|||
package com.example.alzapp;
|
||||
|
||||
import android.content.res.ColorStateList;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Button;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class QuizQuestionPage extends AppCompatActivity {
|
||||
|
||||
private TextView textViewQuestion;
|
||||
private TextView textViewScore;
|
||||
private TextView textViewQuestionCount ;
|
||||
private TextView textViewCountDown;
|
||||
private RadioGroup rbGroup;
|
||||
private RadioButton rb1;
|
||||
private RadioButton rb2;
|
||||
private RadioButton rb3;
|
||||
private Button buttonConfirmNext;
|
||||
|
||||
private ColorStateList textColorDefaultRb;
|
||||
|
||||
|
||||
private List<Question> questionList;
|
||||
private int questionCounter;
|
||||
private int questionCountTotal;
|
||||
private Question currentQuestion;
|
||||
|
||||
private int score;
|
||||
private boolean answered;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_quiz_question_page);
|
||||
|
||||
textViewQuestion = findViewById(R.id.quiz_question);
|
||||
textViewScore = findViewById(R.id.quiz_score);
|
||||
textViewQuestionCount = findViewById(R.id.quiz_question_counter);
|
||||
textViewCountDown = findViewById(R.id.quiz_Timer);
|
||||
rbGroup = findViewById(R.id.quiz_radio_group);
|
||||
rb1 = findViewById(R.id.radioButton30);
|
||||
rb2 = findViewById(R.id.radioButton31);
|
||||
rb3 = findViewById(R.id.radioButton32);
|
||||
buttonConfirmNext = findViewById(R.id.quiz_confirm_next);
|
||||
|
||||
QuizDbHelper dbHelper = new QuizDbHelper(this);
|
||||
questionList = dbHelper.getAllQuestions();
|
||||
questionCountTotal = questionList.size();
|
||||
Collections.shuffle(questionList);
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
else {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,8 +11,8 @@
|
|||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="120dp"
|
||||
android:layout_height="80dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="41dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
|
@ -28,13 +28,15 @@
|
|||
android:id="@+id/include"
|
||||
layout="@layout/backbutton"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:layout_editor_absoluteX="0dp" />
|
||||
|
||||
|
||||
<Button
|
||||
android:id="@+id/tilematch"
|
||||
android:layout_width="140dp"
|
||||
android:layout_height="140dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginTop="70dp"
|
||||
android:layout_marginEnd="36dp"
|
||||
android:background="@drawable/box_design"
|
||||
android:text="TILE MATCHING"
|
||||
|
@ -48,7 +50,7 @@
|
|||
android:id="@+id/game4"
|
||||
android:layout_width="140dp"
|
||||
android:layout_height="140dp"
|
||||
android:layout_marginTop="29dp"
|
||||
android:layout_marginTop="70dp"
|
||||
android:layout_marginEnd="36dp"
|
||||
android:background="@drawable/box_design"
|
||||
android:text="Game 4"
|
||||
|
@ -63,7 +65,7 @@
|
|||
android:layout_width="140dp"
|
||||
android:layout_height="140dp"
|
||||
android:layout_marginStart="36dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginTop="70dp"
|
||||
android:background="@drawable/box_design"
|
||||
android:text="JUMBLED WORDS"
|
||||
android:textSize="16sp"
|
||||
|
@ -79,7 +81,7 @@
|
|||
android:layout_width="140dp"
|
||||
android:layout_height="140dp"
|
||||
android:layout_marginStart="36dp"
|
||||
android:layout_marginTop="29dp"
|
||||
android:layout_marginTop="70dp"
|
||||
android:background="@drawable/box_design"
|
||||
android:text="quiz"
|
||||
android:textSize="16sp"
|
||||
|
@ -88,5 +90,23 @@
|
|||
app:layout_constraintTop_toBottomOf="@+id/jumble"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/username"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="100dp"
|
||||
android:layout_marginTop="40dp"
|
||||
android:layout_marginEnd="400dp"
|
||||
android:layout_marginBottom="35dp"
|
||||
android:text="Username:"
|
||||
android:textColor="@android:color/black"
|
||||
android:textSize="20sp"
|
||||
app:layout_constraintBottom_toTopOf="@+id/jumble"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.259"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/include"
|
||||
app:layout_constraintVertical_bias="0.976" />
|
||||
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -23,6 +23,7 @@
|
|||
android:layout_below="@+id/quiz_score" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/quiz_Timer"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="00:30"
|
||||
|
@ -63,7 +64,7 @@
|
|||
android:id="@+id/radioButton31"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Option 1"
|
||||
android:text="Option 2"
|
||||
android:textSize="22sp"/>
|
||||
|
||||
|
||||
|
@ -71,7 +72,7 @@
|
|||
android:id="@+id/radioButton32"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Option 1"
|
||||
android:text="Option 3"
|
||||
android:textSize="22sp"/>
|
||||
|
||||
</RadioGroup>
|
||||
|
@ -79,7 +80,7 @@
|
|||
|
||||
|
||||
<Button
|
||||
android:id="@+id/quiz_question_next"
|
||||
android:id="@+id/quiz_confirm_next"
|
||||
android:layout_width="160dp"
|
||||
android:layout_height="50dp"
|
||||
android:text="next"
|
||||
|
|
Loading…
Reference in New Issue