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

  • SEARCH
  • Home
  • 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 7903351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:43:38+00:00 2026-06-03T09:43:38+00:00

In an activity I load rows of a listview which takes much time, therefore

  • 0

In an activity I load rows of a listview which takes much time, therefore I put this task in a separate thread to allow displaying a progressDialog.

I do the following

private void doMyStuff() {
    listItems.clear();
    progressDialog.show();

    new Thread(new Runnable() {
        @Override
        public void run() {                
            for () {
                listItems.add(something to add);
            }
        handler.sendEmptyMessage(0);
        progressDialog.dismiss();
        }
    }).start();
}

private Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        if (msg.what == 0) {
            adapter.notifyDataSetChanged();
        }
    };
};

I have sometimes a bug which raises an IllegalStateException. First of all I was surprised, because programming thread like that is what I usually do in standard Java programs.

The bug appears only “sometimes” and it does not appear when doing step by step debugging.

This lead me to search around the web and I found some questions in SO related to this and I must admit that things are not clear to my mind.

As I call the notifyDataSetChanged() only when the thread finished why does it sometimes raises an exception.

Can someone confirm me that this way of doing is wrong, and that I MUST use async task and perhaps explain me why ???

I need to have a progressDialog displayed, can someone give me a simple example of AsyncTask populating a listview AND displaying a progressDialog of the populating progress.

Thanks

UPDATE

jtanveer gave me the answer to the asynctask question. Now the others pointed out that the dismiss is not in the handler, which I correct.

According to the article given by jtanveer on “Painless Threading” they say that

Android offers several ways to access the UI thread from other threads which one of them is HANDLER.

Does someone know why putting the dismissed in the handler did not solve my problem ? for me listItem.add has nothing to do with UI ? Am I wrong on that point ?

For me, in my code the only UI is adapter and progressdialog ? Any commentary or explanation is welcome.

FINAL ANSWER

stjom gave a working answer for my specific code. Running the runOnUiThread in the handler. It’s working but I am surprised because I thought the handler was run in the Ui Thread …

Thanx to all for all your answers.

  • 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-03T09:43:40+00:00Added an answer on June 3, 2026 at 9:43 am

    whenever you call adapter.notifyDataSetChanged(); it identifies any changes to your listItems object. if any change is found, it will update the UI accordingly which I think causes your problem. you can call

    runOnUiThread(new Runnable() {
        public void run() {
            adapter.notifyDataSetChanged();
        }  
    });
    

    inside your handler.

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

Sidebar

Related Questions

I'm trying to get the performSelector to load the activity indicator on a separate
I want to create the activity which will load the images from Web by
I have a ListActivity which launches another Activity based on the list selection. This
I have a ListView on main activity, which shows a Contact picture on the
I included a WebView in my activity and load some Javascript in it which
I have list of items in my activity which I load dynamically, item after
I found this code in SO to show ProgressDialog while load Activity : progDailog
In activity I load preferences like: public void LoadFontSize(){ sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); loadedFontSize =
I have various pieces of generated HTML/JS that I load into Activity with WebView#loadDataWithBaseURL.
I have an activity that lets the user load some data when they press

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.