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.
source share