diff --git a/app/build.gradle b/app/build.gradle index 661b660..597c59a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,5 +32,6 @@ dependencies { implementation "com.google.code.gson:gson:2.8.2" annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" annotationProcessor "android.arch.persistence.room:compiler:$lifecycle_version" - compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' + } + diff --git a/app/src/main/java/com/example/multiplechoice/Answer.java b/app/src/main/java/com/example/multiplechoice/Answer.java new file mode 100644 index 0000000..397baaa --- /dev/null +++ b/app/src/main/java/com/example/multiplechoice/Answer.java @@ -0,0 +1,48 @@ +package com.example.multiplechoice; + +public class Answer { + private static String answer1; + private static String answer2; + private static String answer3; + private static String answer4; + private static String correctAnswer; + + + public Answer(String answer1, String answer2, String answer3, String answer4, String correctAnswer) { + this.answer1 = answer1; + this.answer2 = answer2; + this.answer3 = answer3; + this.answer4 = answer4; + this.correctAnswer = correctAnswer; + } + + public static String getAnswer1() { + return answer1; + } + + public static String getAnswer2() { + return answer2; + } + + public static String getAnswer3() { + return answer3; + } + + public static String getAnswer4() { + return answer4; + } + + public static String getCorrectAnswer() { + return correctAnswer; + } + + + public boolean isNull(String answer4){ + boolean check = false; + if(answer4 == null){ + check = true; + } + return check; + } + +} diff --git a/app/src/main/java/com/example/multiplechoice/MainActivity.java b/app/src/main/java/com/example/multiplechoice/MainActivity.java index a7d568e..2b642e8 100644 --- a/app/src/main/java/com/example/multiplechoice/MainActivity.java +++ b/app/src/main/java/com/example/multiplechoice/MainActivity.java @@ -3,47 +3,125 @@ import androidx.appcompat.app.AppCompatActivity; import android.os.AsyncTask; import android.os.Bundle; +import android.view.View; import android.widget.Button; import android.widget.TextView; import org.json.JSONException; +import org.w3c.dom.Text; + import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.concurrent.ExecutionException; -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity implements View.OnClickListener { FetchQ fetchq = new FetchQ(); List questions = null; - String correctAnswer = null; + String correctAnswer; + private Bundle savedInstanceState; + int iterator = 0; + Question question; @Override protected void onCreate(Bundle savedInstanceState) { + this.savedInstanceState = savedInstanceState; super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); - TextView tQuestion = findViewById(R.id.textViewQustion); - Button option1 = findViewById(R.id.bOption1); - Button option2 = findViewById(R.id.bOption2); - Button option3 = findViewById(R.id.bOption3); - Button option4 = findViewById(R.id.bOption4); + TextView tQuestion = findViewById(R.id.textViewQuestion); + Button answer1 = findViewById(R.id.bAnswer1); + Button answer2 = findViewById(R.id.bAnswer2); + Button answer3 = findViewById(R.id.bAnswer3); + Button answer4 = findViewById(R.id.bAnswer4); + + answer1.setOnClickListener(this); + answer2.setOnClickListener(this); + answer3.setOnClickListener(this); + answer4.setOnClickListener(this); + + + + //Retrieves all the questions from the database and shuffles it to create a random order of presentation for the questions try { questions = fetchq.execute().get(); - System.out.println(questions); Collections.shuffle(questions); - } catch (ExecutionException e) { - e.printStackTrace(); - } catch (InterruptedException e) { + } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); } + + this.question = loadQuestionElements(questions, iterator, tQuestion, answer1, answer2, answer3, answer4); + this.correctAnswer = question.getCorrectAnswer(); + + } + + + public Question loadQuestionElements(List questions, int iterator, TextView question, Button option1, Button option2, Button option3, Button option4){ + Question q = questions.get(iterator); + question.setText(q.getQuestion()); + option1.setText(q.getOption1()); + option2.setText(q.getOption2()); + option3.setText(q.getOption3()); + option4.setText(q.getOption4()); + this.correctAnswer = q.getCorrectAnswer(); + this.iterator++; + return q; } + @Override + public void onClick(View v) { + Button button = (Button) v; + + switch(v.getId()){ + case R.id.bAnswer1: + if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){ + button.setBackgroundColor(0xFF4CAF50);//Set button background color to green + }else{ + button.setBackgroundColor(0xFFF44336);//Set button background color to red + } + + + + + case R.id.bAnswer2: + if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){ + button.setBackgroundColor(0xFF4CAF50);//Set button background color to green + }else{ + button.setBackgroundColor(0xFFF44336);//Set button background color to red + } + + + case R.id.bAnswer3: + if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){ + button.setBackgroundColor(0xFF4CAF50);//Set button background color to green + }else{ + button.setBackgroundColor(0xFFF44336);//Set button background color to red + } + case R.id.bAnswer4: + if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){ + button.setBackgroundColor(0xFF4CAF50);//Set button background color to green + }else{ + button.setBackgroundColor(0xFFF44336);//Set button background color to red + } + + + + + + + + } + + } + + private class FetchQ extends AsyncTask> { List questions; @@ -52,23 +130,24 @@ private class FetchQ extends AsyncTask> { @Override protected List doInBackground(Void... voids) { try { - this.questions = Question.fetchQuestions(); - } catch (JSONException e) { - e.printStackTrace(); - } catch (IOException e) { + this.questions = Question.fetchQuestions(); + } catch (JSONException | IOException e) { e.printStackTrace(); } - return questions; + return questions; } - } - public void setList(List questions){ - this.questions=questions; } + + + + + } + diff --git a/app/src/main/java/com/example/multiplechoice/Question.java b/app/src/main/java/com/example/multiplechoice/Question.java index 3189aa9..2792abd 100644 --- a/app/src/main/java/com/example/multiplechoice/Question.java +++ b/app/src/main/java/com/example/multiplechoice/Question.java @@ -40,6 +40,7 @@ public Question (int i, String q, String o1, String o2, String o3, String o4, St public String getQuestion() { return question; } + public int getId() { return id; } @@ -64,9 +65,21 @@ public String getCorrectAnswer() { return correctAnswer; } + public boolean isCorrect(String answer,String correctAnswer){ + boolean value; + if (answer.equals(correctAnswer)){ + value = true; + }else{ + value = false; + } + return value; + } + + + public static List fetchQuestions () throws JSONException, IOException { - HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost/icd_tei_ser/script/question/get_questions.php").openConnection(); + HttpURLConnection connection = (HttpURLConnection) new URL("http://192.168.1.6/icd_tei_ser/script/question/get_questions.php").openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-length", "0"); connection.setUseCaches(false); @@ -107,6 +120,8 @@ public static List fetchQuestions () throws JSONException, IOException } + + } diff --git a/app/src/main/java/com/example/multiplechoice/Timer.java b/app/src/main/java/com/example/multiplechoice/Timer.java new file mode 100644 index 0000000..295cd58 --- /dev/null +++ b/app/src/main/java/com/example/multiplechoice/Timer.java @@ -0,0 +1,19 @@ +package com.example.multiplechoice; + +import android.os.CountDownTimer; + +public class Timer extends CountDownTimer { + public Timer(long millisInFuture, long countDownInterval) { + super(millisInFuture, countDownInterval); + } + + @Override + public void onTick(long millisUntilFinished) { + + } + + @Override + public void onFinish() { + + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d5d5d1c..9689c12 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,100 +6,86 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - - + android:textAlignment="viewEnd" + app:layout_constraintEnd_toEndOf="@+id/textViewQuestion" + app:layout_constraintTop_toBottomOf="@+id/textViewQuestion" /> + app:layout_constraintStart_toStartOf="@+id/textViewQuestion" + app:layout_constraintTop_toBottomOf="@+id/textViewQuestion" /> + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="0.03" /> - + app:layout_constraintStart_toStartOf="@+id/textViewQuestion" + app:layout_constraintTop_toBottomOf="@+id/textViewQuestion" /> - + app:layout_constraintStart_toStartOf="@+id/bAnswer1" + app:layout_constraintTop_toBottomOf="@+id/bAnswer1" /> - +