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 7765653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:13:07+00:00 2026-06-01T15:13:07+00:00

I am trying to fetch data from internet, parse it and then show the

  • 0

I am trying to fetch data from internet, parse it and then show the data to the user. The action usually takes 3 seconds or so, and I would like to a spinner ProgressDialog show a message saying that I am loading data. The problem is that ProgressDialog only shows up when the loading is almost complete. This is my code

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button load_routes = (Button)findViewById(R.id.load_routes);

    load_routes.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            task_complete = false;
            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(false);
            progressBar.setMessage("Fetching routes data ...");
            progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressBar.show();

            if(haveNetworkConnection())
            {
                loading_routes_data(v);
            }
            else
            {
                progressBar.dismiss();
            }
        }
    });
}

And this is my function that gets called onclick

private void loading_routes_data(View v)
{
    try
    {
        new Thread(new Runnable()
        {
            public void run()
            {
                while(!task_complete)
                {
                    refresh_routes();
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                runOnUiThread(new Runnable()
                {
                    public void run()
                    {
                        progressBar.dismiss();
                    }
                });
            }
        }).start();
    }
    catch(Exception e)
    {
    }

    while(!task_complete)
    {
    }
}

My refresh_routes() function gets the data, parses it and then sets task_complete to true, I am not even sure on whether or not I should keep it inside the while loop.

So that is my huge problem. I would like to display ProgressDialog right away on button click, then I would like to call refresh_routes(), and once refresh_routes() is complete I would like to get rid of progress dialog. But I am unsure as to how to go about doing that.

If someone could show me how to go about solving the problem with some article as an example or something like that I will be really grateful. Thank you in advance 🙂

  • 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-01T15:13:08+00:00Added an answer on June 1, 2026 at 3:13 pm

    You’re locking the UI thread by calling while(!task_complete) at the end of your method. You really should be using an AsyncTask for an operation like this. Here’s what your AsyncTask should look like:

    private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
    
        private ProgressDialog progressDialog;
        private Context context;
    
        public MyAsyncTask(Context context) {
    
            this.context = context;
        }
    
        @Override
        protected void onPreExecute() {
    
            super.onPreExecute();
            progressDialog = ProgressDialog.show( context, null, "Fetching routes data ..." );
        }
    
        @Override
        protected Void doInBackground( Void... params ) {
    
            while (!task_complete) {
                refresh_routes();
                try {
                    Thread.sleep( 1000 );
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            return null;
        }
    
        @Override
        protected void onPostExecute( Void result ) {
    
            super.onPostExecute( result );
            progressDialog.dismiss();
        }
    }
    

    To execute this, you would do this:

    new MyAsyncTask( this ).execute();
    

    I’m not sure how your refresh_routes() method works, but you might consider moving it’s functionality inside the doInBackground() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to fetch data from a div (based on his id), using PHP's
I'm trying to fetch the XML data from a query to the api without
I am trying to fetch data from DB and loading the DB with the
I'm using an Oracle DB and I'm trying to fetch data from its tables
I am trying to fetch data from my database and when I display it
I am trying to fetch some data from Netflix REST API from my iOS
in my app i am trying to fetch some data from database and i
I am trying to fetch data from database using php and send it to
i am trying to fetch data from SQLite to list view but my list
I'm trying to fetch some data from Yahoo's API and Im running into some

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.