我正在开发一个测验应用,其中我使用RETROFIT
库获取问题,4个选项及其正确答案。我希望在QuizActivity
上填写多个问题和答案。该活动包含下一个和之前的两个按钮,用于获取下一个或上一个问题及其选项,具体取决于相应的onClick
事件。我正在使用JSON
获得正确的retrofit
响应,但无法找到实现我的目标的方法是使用按钮点击事件在QuizActivity
上填充响应。我已经为这个解决方案进行了很多研究,并最终提出了问题。提前谢谢。
以下是QuizActivity.java
的代码:
package com.oskartechnologies.safetymodule;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import org.json.JSONArray;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class QuizActivity extends AppCompatActivity implements View.OnClickListener {
TextView question;
RadioButton optiona, optionb, optionc, optiond;
RadioGroup radioGroup;
Button next, previous;
int questionsCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
optiona = (RadioButton) findViewById(R.id.optiona);
optionb = (RadioButton) findViewById(R.id.optionb);
optiond = (RadioButton) findViewById(R.id.optionc);
optiond = (RadioButton) findViewById(R.id.optiond);
question = (TextView) findViewById(R.id.questionView);
previous = (Button) findViewById(R.id.prevButton);
next = (Button) findViewById(R.id.nextButton);
previous.setOnClickListener(this);
getQuiz();
}
private void getQuiz() {
final BlockingQueue<JSONArray> blockingQueue = new ArrayBlockingQueue<>(1);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://netcafe.oskartechnologies.com/").addConverterFactory(GsonConverterFactory.create()).build();
APiService service = retrofit.create(APiService.class);
Call<List<QuizModule>> call = service.getQuizData("1");
Log.d("retrofit", call + "");
call.enqueue(new Callback<List<QuizModule>>() {
List<QuizModule> list;
@Override
public void onResponse(Call<List<QuizModule>> call, Response<List<QuizModule>> response) {
Log.d("Response", response.message().toString());
list = response.body();
//questionsCount = list.size();
//QuizModule q = new QuizModule();
//for (int i = 0; i < list.size(); i++) {
//String questiontext = list.get(i).getQuestion();
//question.setText(questiontext);
//}
}
@Override
public void onFailure(Call<List<QuizModule>> call, Throwable t) {
Log.d("Failure", t.getMessage().toString());
}
});
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.nextButton) {
if (questionsCount > 0) {
}
}
}
}
以下是POJO
类:
package com.oskartechnologies.safetymodule;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Wasim on 24/01/2018.
*/
public class QuizModule {
@SerializedName("question")
@Expose
private String question;
@SerializedName("option_a")
@Expose
private String optiona;
@SerializedName("option_b")
@Expose
private String optionb;
@SerializedName("option_c")
@Expose
private String optionc;
@SerializedName("option_d")
@Expose
private String optiond;
@SerializedName("answer")
@Expose
private String answer;
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOptiona() {
return optiona;
}
public void setOptiona(String optiona) {
this.optiona = optiona;
}
public String getOptionb() {
return optionb;
}
public void setOptionb(String optionb) {
this.optionb = optionb;
}
public String getOptionc() {
return optionc;
}
public void setOptionc(String optionc) {
this.optionc = optionc;
}
public String getOptiond() {
return optiond;
}
public void setOptiond(String optiond) {
this.optiond = optiond;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
以下是我得到的JSON
回复:
[
{
"question": "If a cream coloured band is located over the fire extinguisher, it indicates that the fire extinguisher is which of the following?",
"option_a": "Water",
"option_b": "Dry Powder",
"option_c": "Foam",
"option_d": "Carbon Dioxide",
"answer": "Foam"
},
{
"question": "Which type of fire extinguish do you use to fight an electrical fire if a carbon dioxide extinguisher is unavailable?",
"option_a": "Water",
"option_b": "Wet Chemical",
"option_c": "Foam",
"option_d": "Dry Powder",
"answer": "Dry Powder"
}
]
答案 0 :(得分:0)
使List<QuizModule> list
成为全局变量,以便您可以在onClick方法中访问它。
还要添加一个全局计数器来跟踪问题索引:int counter
然后在你的onClick:
if (questionsCount > 0) {
String questiontext = list.get(counter).getQuestion();
question.setText(questiontext);
counter++;
}