我
问题
随着名单越来越大,很难回答。我想要的是每个问题都要在一个布局中显示。当用户点击下一个时,它应该加载下一个问题。
QuizView.java
List<Quiz> quizList = new ArrayList<Quiz>();
String json = "[{\"question\": \"What?\", \"option1\": \"alpha\" , \"option2\": \"beta\" , \"option3\": \"gamma\" , \"option4\": \"alpha\"},{\"question\": \"What?\", \"option1\": \"alpha\" , \"option2\": \"beta\" , \"option3\": \"gamma\" , \"option4\": \"alpha\"}]";
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(json).getAsJsonArray();
for(JsonElement obj : array) {
Quiz quiz = gson.fromJson(obj,Quiz.class);
String question = quiz.getQuestion();
System.out.println("Question: "+question);
quizList.add(quiz);
Quiz.java
public class Quiz {
public String question;
public String option1;
public String option2;
public String option3;
public String option4;
public void setQuestion(String question) {
this.question = question;
}
public String getQuestion() {
return question;
}
public void setOption1(String option1) {
this.option1 = option1;
}
public String getOption1() {
return option1;
}
public void setOption2(String option2) {
this.option2 = option2;
}
public String getOption2() {
return option2;
}
public void setOption3(String option3) {
this.option3 = option3;
}
public String getOption3() {
return option3;
}
public void setOption4(String option4) {
this.option4 = option4;
}
public String getOption4() {
return option4;
}
}