I have AsyncTaskone that takes a context (in use onPostExecute) and runs doInBackgroundto return an ArrayList of objects from the server. When I do this, I see that it doInBackgroundworks fine, however it does not pass the result to onPostExecute.
After much searching, I have yet to find an answer on how to return ArrayList objects in AsyncTask.
This is the object that I create in doInBackgroundand using onPostExecute:
public class ServerTimeCard {
public String eventNameInput;
Boolean isLocation, isImage, isVoice;
public ServerTimeCard(String eventNameInput, boolean isLocation, boolean isImage, boolean isVoice) {
this.eventNameInput = eventNameInput;
this.isLocation = isLocation;
this.isImage = isImage;
this.isVoice = isVoice;
}
}
I am performing AsyncTaskwith new LoadFriendCards(context).execute();at onCreate.
Expected Result: doInBackground should return an ArrayList inonPostExecute
. ArrayList<ServerTimeCard> onPostExecute , arraylist doInBackground .
AsyncTask.
public class LoadFriendCards extends AsyncTask<Void, Void, ArrayList<ServerTimeCard>> {
Context context;
ArrayList<ServerTimeCard> result;
public LoadFriendCards(Context context) {
this.context = context;
}
@Override
protected ArrayList<ServerTimeCard> doInBackground(Void... voids) {
result = new ArrayList<ServerTimeCard>();
for (String friend : friendsListNames) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject");
query.whereEqualTo("accountName", friend+"@gmail.com");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (ParseObject cardInfo : objects) {
ServerTimeCard item = new ServerTimeCard(
cardInfo.getString("eventName"),
cardInfo.getBoolean("isImage"),
cardInfo.getBoolean("isImage"),
cardInfo.getBoolean("isVoice"));
result.add(item);
Log.e("New item called: ", String.valueOf(item.eventNameInput));
Log.e("New size of array...", String.valueOf(result.size()));
}
} else {
Log.d("info", "Error: " + e.getMessage());
}
}
});
}
Log.e("Size of passing array", String.valueOf(result.size()));
return result;
}
@Override
protected void onPostExecute(ArrayList<ServerTimeCard> result) {
Log.e("Data list size: ", String.valueOf(result.size()));
}
}
Logcat Log.e: ( , doInBackground, onPostExecute, doInBackground)
E/Size of passing array﹕ 0
E/Data list size:﹕ 0
E/New item called:﹕ yah
E/New size of array...﹕ 1
E/New item called:﹕ lplokolol
E/New size of array...﹕ 2
E/New item called:﹕ It works
E/New size of array...﹕ 3
SOLVED: , AsyncTask, . , . : .