Showing a ProgressDialog while waiting for an attached stream

In my activity, I am loading the contents of the list from the database and want to display the ProgressDialog while it is loading.
I have my own working on it, but if I load data into a stream (what should I do?), The list is displayed before the data is loaded. But if I use join, the ProgressDialog does not even appear.

How can I combine this? Or is this not possible at all with threads? (Perhaps AsyncTask?)

Here is the code for reference:

final ProgressDialog progressD=ProgressDialog.show(ShopSwitchActivity.this, "", "Loading..", true); Thread myThread = new Thread(new Runnable() { @Override public void run() { try { getData(); }catch(Exception e){} } }); myThread.start(); try { myThread.join(); } catch (InterruptedException e) { } progressD.dismiss(); 

EDIT: Updated code using AsyncTask:

 public class LoadList extends AsyncTask<String, Void, Boolean> { ProgressDialog dialog; ShopSwitchActivity activity; public LoadList(ShopSwitchActivity activity) { this.activity = activity; dialog = new ProgressDialog(activity); } protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.show(); } @Override protected void onPostExecute(final Boolean success) { if (dialog.isShowing()) { dialog.dismiss(); } } protected Boolean doInBackground(final String... args) { try{ activity.getData(); } catch (Exception e) { Log.e("error", e.getMessage()); } return true; } } 

Edit: My solution Now using AsyncTask to load data, and after that I update the list with new data.

+1
source share
4 answers

You can do this with AsyncTask. Write an AsyncTask class in your main class that you want to execute. You can create a progress dialog in preexcecute of your async class and fire it in onpostexecute of the async class. Here's how you do it:

  class MyAsync extends AsyncTask<String, Void, Void> { ProgressDialog pd; Context co; MyActivity ma; public MyAsync (MyActivity ma){ this.ma= ma; this.co = ma; pd= new ProgressDialog(co); } @Override protected void onPreExecute() { this.pd.show(); super.onPreExecute(); } @Override protected Void doInBackground(String... params) { // do your database operations here return null; } @Override protected void onPostExecute(Void result) { // show db results and dismiss progress dialog pd.dismiss(); super.onPostExecute(result); } } 

in the call to MyActivity:

 MyActivity ma = this; new MyAsync(ma).execute(); 
+4
source

You seem to have missed the flow point. Stream occurs simultaneously with your application. This way, your application does not trigger a start, and then waits until the thread is full - if you could just use the function. Instead, your code continues to work. Therefore, if you just call right away, you are not doing anything. This way you will bypass NetworkOnMainThreadException, but you will still delay the user interface thread so that your application does not respond completely (and, as a result, does not show a dialog), and you end up crashing when the watchdog timer kills you.

Instead, the best way to handle this is to use AsyncTask. Call getData in doInBackground (). Then release the dialog in onPostExecute.

+1
source

Instead, you should use AsyncTask .

There is a link to the library. It is pretty simple:

1) onPreExecute() = show ProgressDialog

2) doInBackground() = execute your code

3) onPostExecute() = reject ProgressDialog

Here is a good tutorial.

Generally:

 @Override protected void onPreExecute() { dialog = new ProgressDialog(this.context); dialog.setMessage("Loading..."); dialog.setCanceledOnTouchOutside(false); } @Override protected void onPostExecute(String result) { if(dialog.isShowing()) { dialog.dismiss(); } } 
+1
source
 private Thread myThread; private ProgressDialog mProgDialog; mProgDialog = ProgressDialog.show(ShopSwitchActivity.this,"","Laden..", true); myThread= new Thread(new Runnable() { public void run() { myThread.setPriority(Thread.MIN_PRIORITY); try { getData(); }catch(Exception e){} runOnUiThread(new Runnable() { public void run() { if (mProgDialog != null&& mProgDialog.isShowing()) mProgDialog.dismiss(); } } }); } }); myThread.start(); 
+1
source

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


All Articles