package com.example.programingquiz; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import com.google.android.material.snackbar.Snackbar; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.Collections; public class BeginnerTaskActivity extends AppCompatActivity { TextView questionTV, contentTV; Button aBtn, bBtn; CategoryActivity.DifficultyType difficultyType; String login, taskCategory; LoginActivity.LoginType loginType; long taskID; @Override public void onBackPressed() {} @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_beginner_task); questionTV = findViewById(R.id.questionTV); contentTV = findViewById(R.id.contentTV); aBtn = findViewById(R.id.aBtn); bBtn = findViewById(R.id.bBtn); aBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { openReportActivity(aBtn.getText().toString()); } }); bBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { openReportActivity(bBtn.getText().toString()); } }); getIntentExtras(); drawTask(); } private void getIntentExtras(){ Intent intent = getIntent(); login = intent.getStringExtra("login"); loginType = (LoginActivity.LoginType) intent.getSerializableExtra("type"); taskCategory = intent.getStringExtra("taskCategory").toLowerCase(); difficultyType = (CategoryActivity.DifficultyType) intent.getSerializableExtra("difficultyType"); } private void drawTask(){ String url = StartActivity.URL + "api/drawTask/" + taskCategory + "/" + difficultyType.toString(); RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() { @Override public void onResponse(JSONObject response) { try { questionTV.setText(response.getString("question")); taskID = response.getInt("id"); contentTV.setText(response.getString("content").replace("\\n", "\n").replace("\\t", "\t")); JSONArray array = response.getJSONArray("answers"); ArrayList answers = shuffleAnswers(array); aBtn.setText(answers.get(0)); bBtn.setText(answers.get(1)); } catch (JSONException e) { e.printStackTrace(); } } }, new com.android.volley.Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Snackbar.make(findViewById(android.R.id.content), "some error occurred, please try again later", Snackbar.LENGTH_LONG).show(); String errorMsg = error.toString(); System.out.println(errorMsg); } }); queue.add(request); } private ArrayList shuffleAnswers(JSONArray answers){ ArrayList result = new ArrayList<>(); for(int i = 0; i < answers.length(); i++) { try { result.add(answers.getString(i)); } catch (JSONException e) { e.printStackTrace(); } } Collections.shuffle(result); return result; } private void openReportActivity(String userAnswer){ Intent intent = new Intent(this, ReportActivity.class); intent.putExtra("login", login); intent.putExtra("type", loginType); intent.putExtra("taskID", taskID); intent.putExtra("difficultyType", difficultyType); intent.putExtra("taskCategory", taskCategory); intent.putExtra("userAnswer", userAnswer); intent.putExtra("aAnswer", aBtn.getText().toString()); intent.putExtra("bAnswer", bBtn.getText().toString()); startActivity(intent); } }