Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess a custom class from a new thread in a ProgressDialog on Android
    text
    copied!<p>I have created a ProgressDialog in android and it works when I do a simple example. </p> <p>For example, this works.</p> <pre><code>public void onClick(View v) { // Perform action on click System.out.println("Progess Bar"); final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this, "Please wait...", "Getting updates...", true); new Thread() { public void run() { try { // Do some Fake-Work sleep(5000); } catch (Exception e) { } // Dismiss the Dialog myProgressDialog.dismiss(); } }.start(); } </code></pre> <p>But once I add in a reference to my custom class, it just stops running this new thread. </p> <pre><code> button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click System.out.println("Progess Bar"); // Display an indeterminate Progress-Dialog final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this, "Please wait...", "Getting Updates...", true); new Thread() { public void run() { try { HealthySubObject hsObject = new HealthySubObject(); // Do some more work with my hsObject - nothing happens after this point. sleep(5000); } catch (Exception e) { } // Dismiss the Dialog myProgressDialog.dismiss(); } }.start(); } }); </code></pre> <p>What happens is that as soon as I click this button, the progress dialog flashes up on the screen real quick and then disappears. But if you look at my code, it should wait 5 seconds before disappearing. I have put debug statements before and after the reference to my custom class and I can see the statements before but not the ones after. Does anyone have any idea why that is happening? As long as my class is public I should be able to call it from a new thread, right? </p> <p>I am still pretty new to android and this is my first adventure into multi-threaded android apps. Any help would be much appreciated. </p> <p><em><strong>SOLUTION</em></strong></p> <p>Thanks for your help everyone. It is working now. </p> <pre><code> button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click System.out.println("Progess Bar"); //ProgressDialog dialog = ProgressDialog.show(AndroidTestApplicationActivity.this, "", "Loading. Please wait...", true); // Display an indeterminate Progress-Dialog final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this, "Please wait...", "Doing Extreme Calculations...", true); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { HealthySubObject hsObject = new HealthySubObject(); ArrayList&lt;HashMap&lt;String, String&gt;&gt; onlineDB = hsObject.jsonToArray(); // // more stuff goes here. // // myProgressDialog.dismiss(); } }, 1500); } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload