What is the need of creating the list when we have already created the ArrayList to store the JSONArray? This code is to access the data from the internet which is JSON format, using the volley library. I am having a little problem in understanding the use of ArrayLists along with List. Couldn't we just exclude the List?
//What is the use of the List?
ArrayList<Question> questionArrayList= new ArrayList<>();
public List<Question> getQuestions (){
JsonArrayRequest jsonArrayRequest =new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i=0;i<response.length();i++){
Question question = new Question();
try {
question.setQuestionId(response.getJSONArray(i).getString(0));
question.setTorF(response.getJSONArray(i).getBoolean(0));
questionArrayList.add(question);//If we had to add the question entries to the questionArrayList then why create a list(getQuestions)?
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
};
//My Question class is this:-
package com.example.trivia.model;
public class Question {
private String QuestionId;
private boolean IsTorF;
public Question(String questionId, boolean isTorF) {
this.QuestionId = questionId;
this.IsTorF = isTorF;
}
public Question(){
}
public String getQuestionId() {
return QuestionId;
}
public void setQuestionId(String questionId) {
QuestionId = questionId;
}
public boolean isTorF() {
return IsTorF;
}
public void setTorF(boolean torF) {
IsTorF = torF;
}
}