Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8059671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:44:50+00:00 2026-06-05T09:44:50+00:00

I am using a AsyncTask (1) in another AsyncTask (2). AsyncTask 1 fetches online

  • 0

I am using a AsyncTask (1) in another AsyncTask (2).
AsyncTask 1 fetches online user data, counts the number of entries in the response and for each entry, in onPostExecute displays a username and runs a new AsyncTask (2) to fetch a image from a server and load it into a ImageView. This all happens in onPostExecute.
This is working flawlessly, the user data is fetched and shown, and the images are shown one by one for each entry.

However, the itteration through the array and the updating of the TextView in AsyncTask 1’s onPostExecute happens so fast, it basically only shows the last user name in the array, the other ones are loaded, but impossible to detect with the human eye 🙂

Meanwhile, AsyncTask 2 is still fetching images from online, and showing profile images for the wrong users.
The problem I have here obviously, is these 2 need to be in sync.
So I thought I just wait for the output in AsyncTask 2 with the get() method, but now nothing is updated at all anymore, no TextView…this is unexpected behaviour for me.

So, the question is how to sync the 2 AsyncTasks?

bit of code to clarify, if it’s still needed

    //instantiate first AsyncTask
    new AsyncRequest().execute(bundle);

    private class AsyncRequest extends AsyncTask<Bundle, Void, String> {
    protected String doInBackground(Bundle... bundle) {
        String data = null;
        try {
            data = request(null, bundle[0]); //request the data
            return data;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }// end method

    protected void onPostExecute(String response) {
        JSONArray data = null;
        try {
            JSONObject response2 = Util.parseJson(response);
            data        = response2.optJSONArray("data");
            int amount  = data.length();
            TextView s1 = (TextView) findViewById(R.id.some_id);
            s1.setText("" + amount); //displays number of items

            //display the data
            for(int i=0; i<amount; i++){
                String email        = "";
                String id           = "";
                JSONObject json_obj = data.getJSONObject(i);
                Log.d("JSONObject ", ""+json_obj);
                String name         = json_obj.getString("name");
                if (json_obj.has("email")){
                    email           = json_obj.getString("email");
                }
                if (json_obj.has("id")){
                    id          = json_obj.getString("id");
                }
                String picture  = "http://www.domain.com/"+id+"/picture";
                TextView s2     = (TextView) findViewById(R.id.name_placeholder);
                s2.setText(name);
                //here we do a new AsynTask for each entry and wait until the data is fetched
                new DownloadProfileImageTask().execute(picture, name).get(); 
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }// end method
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:44:51+00:00Added an answer on June 5, 2026 at 9:44 am

    It is not quite clear why you are calling setText of the single TextView with the name of multiple names. As you have mentioned, although you had setText of all the names, you only see a single name. May be you need to use a ListView or something like that.

    Now regarding your question: probably you don’t need two AsyncTasks. You can do everything in a single AsyncTask. The code will be something like below:

    //Create a Holder class as a data holder. 
    //For simplicity, public attributes are used
    class Holder{
      public String name;
      public String email;
      public String id;
      public BitmapDrawable imageDrawable;
    }
    
    //instantiate the AsyncTask
    new AsyncRequest().execute(bundle);
    
    private class AsyncRequest extends AsyncTask<Bundle, Holder, Integer> {
    protected Integer doInBackground(Bundle... bundle) {
        int amount = 0;
        try {
            data = request(null, bundle[0]); //request the data
    
            JSONArray data = null;
            JSONObject response2 = Util.parseJson(response);
            data        = response2.optJSONArray("data");
            amount  = data.length();
    
            //display the data
            for(int i=0; i<amount; i++){
                Holder holder = new Holder();
                holder.email        = "";
                holder.id           = "";
                JSONObject json_obj = data.getJSONObject(i);
                Log.d("JSONObject ", ""+json_obj);
                holder.name         = json_obj.getString("name");
                if (json_obj.has("email")){
                    holder.email           = json_obj.getString("email");
                }
                if (json_obj.has("id")){
                    holder.id          = json_obj.getString("id");
                }
                String picture  = "http://www.domain.com/"+id+"/picture";
    
                //Fetch the image and create a Drawable from it - Synchronously
                holder.imageDrawable = getImageDrawable(picture, name);
    
                publishProgress(holder);
    
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return amount;
    }// end method
    
    protected void onProgressUpdate(Holder... holder) {
        //Update the user name and image
        TextView s2     = (TextView) findViewById(R.id.name_placeholder);
        s2.setText(holder[0].name);
    
        ImageView imgView = (ImageView) findViewById(R.id.imageViewId);
        imgView.setImageDrawable(holder[0].imageDrawable);
    
    }
    
    protected void onPostExecute(Integer amount) {
        TextView s1 = (TextView) findViewById(R.id.some_id);
        s1.setText(amount.toString()); //displays number of items
    }// end method
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using AsyncTask s to fetch data in response to the user pressing a
i am downloading data from website using asynctask and my code for async task
I am using AsyncTask to get data from a server and want to show
How to create and update listview using AsyncTask getting data from server.Actually i am
In my app i have a processDialog (using AsyncTask) and at doInbackground it fetches
I'm using using AsyncTask to download data over internet and I have a little
i'm using AsyncTask to retrive data from RSS ,,i'm showing a progress Dialog in
I want to download table's data in backgroud so i did using AsyncTask.The preoblem
I am trying to download multiple file using AsyncTask. I start each download in
I have been using Asynctask most of time, but in the current app the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.