Asynctask ArrayList does not go from doInBackground to onPostExecute

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>();

                     // ...a bunch of data retrieval goes on here...    

                     // querying parse for object info
                     // adding a new object to the local ArrayList for all in db
                     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());
                }
            }
        });
    }

                // returning the new ArrayList<ServerTimeCard> to onPostExecute

                // PROBLEM: THE SIZE OF THE RESULT IS 0
                Log.e("Size of passing array", String.valueOf(result.size()));

                return result;
            }

            @Override
            protected void onPostExecute(ArrayList<ServerTimeCard> result) {

                // PROBLEM: This returns 0
                Log.e("Data list size: ", String.valueOf(result.size())); 

                // trying to use all objects in the arraylist here but it doesn't work
                // due to the size. Something isn't passing correctly.

            }
        }

Logcat Log.e: ( , doInBackground, onPostExecute, doInBackground)

E/Size of passing array0
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, . , . : .

+4
4

, findInBackground . . , async async.

  • doInBackground
  • findInBackground → ,
  • onPostExecute .
  • done , .

, , findInBackground AsyncTask ( AsyncTask), find.

+1

result . , , , , . .

+1

( async), query.findInBackground() , . , , .

, AsyncTask , . , , FindCallback done().

, , done().

0

You do not need to do doInBackground to return an arraylist that can be used in onPostExecute. Just return "ok" or "error". Declare and create a private arraylist as a member of your request. Then fill it in doInBackground and use it in onPostexecute.

0
source

Source: https://habr.com/ru/post/1538793/


All Articles