Dears,
Take a look at the codes below...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
url = "http://xxxxxxx/getAllQuestionsByDate.php"
System.out.println(useData());
}
public ArrayList<QuestionsModel> useData() {
ArrayList<QuestionsModel> Temp_List_Of_Model_Objects = new ArrayList<>();
DownloadJson jsonn = new DownloadJson();
jsonn.GetAllQuestionsByDate(url, new JsonCallback() {
@Override
public void onNetworkCallFinished(final JSONObject jsonResponse) {
try {
JSONArray arryKey = jsonResponse.getJSONArray("questions");
int jsonArrayObjectsCount = arryKey.length();
for (int i=0; i < jsonArrayObjectsCount; i++){
JSONObject ArrayRow = arryKey.getJSONObject(i);
int qid = ArrayRow.getInt("QID");
String question_text = ArrayRow.getString("Question");
int questionType = ArrayRow.getInt("Question_Type");
String question_image_url = ArrayRow.getString("Question_Img");
String question_date_inserted = ArrayRow.getString("Question_Insert_Date");
QuestionsModel question = new QuestionsModel(qid, question_text, questionType, question_image_url, question_date_inserted);
Temp_List_Of_Model_Objects.add(question);
}
} catch (JSONException e) {
e.printStackTrace();
}
//System.out.println(List_Of_Model_Objects.get(0).getQuestion_text());
}
});
return Temp_List_Of_Model_Objects;
}
When calling useDate() in OnCreate method, it is returning an empty array []. Why is that? It should be populated with ArrayList data. Am i missing something? According to some debugging, i noticed that the array is populated and then destroyed after 'onNetworkCallFinished' is finished being called.