我想将this json数据保留在数组中。 “问题”必须是一个数组,“选择”和“正确”也必须是一个数组。当我单击按钮时,应该更改值。如您所知,我尝试进行测验应用。该代码有效,但是当我使btnFirst.setText(choiceList.get(4));
不起作用时。
MainActivity.java代码
public class MainActivity extends AppCompatActivity {
RequestQueue requestQueue;
TextView txtQuestion;
Button btnFirst, btnSecond, btnThird, btnFourth;
ArrayList<String> questionList = new ArrayList<>();
ArrayList<String> choiceList = new ArrayList<>();
ArrayList<Boolean> correctList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestQueue = Volley.newRequestQueue(this);
txtQuestion = findViewById(R.id.txtQuestion);
btnFirst = findViewById(R.id.btnFirst);
btnSecond = findViewById(R.id.btnSecond);
btnThird = findViewById(R.id.btnThird);
btnFourth = findViewById(R.id.btnFourth);
String url = "https://private-anon-a98702efdd-quizmasters.apiary-mock.com/questions";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try{
for(int i = 0; i < response.length(); i++) {
JSONObject questionObject = response.getJSONObject(i);
String question = questionObject.getString("question");
questionList.add(question);
txtQuestion.setText(questionList.get(0));
JSONArray choicesArray = questionObject.getJSONArray("choices");
for(int j = 0; j < choicesArray.length(); j++) {
JSONObject currentObject = choicesArray.getJSONObject(j);
String choice = currentObject.getString("choice");
boolean correct = currentObject.getBoolean("correct");
choiceList.add(choice);
correctList.add(correct);
}
btnFirst.setText(choiceList.get(0));
btnSecond.setText(choiceList.get(1));
btnThird.setText(choiceList.get(2));
btnFourth.setText(choiceList.get(3));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("myusarisoy", "" + error);
}
});
requestQueue.add(jsonArrayRequest);
}
}
答案 0 :(得分:1)
请创建模型类以保存所有数据。
public class MyPojo
{
private Choices[] choices;
private String question;
public Choices[] getChoices ()
{
return choices;
}
public void setChoices (Choices[] choices)
{
this.choices = choices;
}
public String getQuestion ()
{
return question;
}
public void setQuestion (String question)
{
this.question = question;
}
@Override
public String toString()
{
return "ClassPojo [choices = "+choices+", question = "+question+"]";
}
}
public class Choices
{
private String correct;
private String choice;
public String getCorrect ()
{
return correct;
}
public void setCorrect (String correct)
{
this.correct = correct;
}
public String getChoice ()
{
return choice;
}
public void setChoice (String choice)
{
this.choice = choice;
}
@Override
public String toString()
{
return "ClassPojo [correct = "+correct+", choice = "+choice+"]";
}
}
现在像下面一样解析您的数据...
ArrayList<MyPojo> allData = new ArrayList<>();
for (int i = 0; i > response.length(); i++) {
JSONObject object = response.getJSONObject(i);
MyPojo myPojo = new MyPojo();
String question = object.optString("question");
myPojo.setQuestion(question);
JSONArray choicesArray = object.getJSONArray("choices");
Choices[] choices = new Choices[choicesArray.length()];
for (int j = 0; j < choicesArray.length(); j++) {
JSONObject choiceObj = choicesArray.getJSONObject(j);
String choice = choiceObj.optString("choice");
choices[j].setChoice(choice);
String correct = choiceObj.optString("correct");
choices[j].setCorrect(correct);
}
myPojo.setChoices(choices);
allData.add(myPojo);
现在将此数组列表传递给您的适配器。
在适配器中获取问题和选择。
String question=allData.get(position).getQuestion.
Choice choice[]=allData.get(poistion).getChoices().
运行选择循环查找选择并从选择中更正
快乐代码:)
答案 1 :(得分:0)
您可以创建一个在给定索引的情况下迭代问题的方法。
public void nextQuestion(int index){
index++;
txtQuestion.setText(questionList.get(index));
btnFirst.setText(choiceList.get(index * 4));
btnSecond.setText(choiceList.get(index * 4 + 1));
btnThird.setText(choiceList.get(index * 4 + 2));
btnFourth.setText(choiceList.get(index * 4 + 3));
}
定义一个变量以跟踪当前问题。
public int index = 0;
还如下更改OnResponse
方法。
@Override
public void onResponse(JSONArray response) {
try{
for(int i = 0; i < response.length(); i++) {
JSONObject questionObject = response.getJSONObject(i);
String question = questionObject.getString("question");
questionList.add(question);
JSONArray choicesArray = questionObject.getJSONArray("choices");
for(int j = 0; j < choicesArray.length(); j++) {
JSONObject currentObject = choicesArray.getJSONObject(j);
String choice = currentObject.getString("choice");
boolean correct = currentObject.getBoolean("correct");
choiceList.add(choice);
correctList.add(correct);
}
}
nextQuestion(index);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在按钮的onClickListener
中将其用作。
nextQuestion(index);