Progress Dialog is not displaying while getting data from separate thread class

后端 未结 2 1037
你的背包
你的背包 2021-01-21 10:05

I am using separate thread to get the json object from the url. but the problem is I would like to show the progress dialog while getting the result. I have create a progress di

相关标签:
2条回答
  • 2021-01-21 10:33

    Instead of you creating and handling a separate thread by yourself, use Asynctask. It will make your job easier. You can have a progress dialog displayed through out the execution of this async task and you can update the progress dialog from asynctask using publishProgress()

    0 讨论(0)
  • 2021-01-21 10:51

    You can do it using AsyncTask..by adding the thread part into the doInBackground method..show the progress dialog in onPreExecute and dismiss it in onPostExecute..

    ex:

    public class MyTask extends AsyncTask<String, Void, String> {
    
    private Context context;
    private ProgressDialog progressDialog;
    
    public MyTask (Context context) {
        this.context = context;
    }
    
    @Override
    protected void onPreExecute() {
            progressDialog = ProgressDialog.show(context, "", "Loading...", true);
    }
    
    /* 
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected String doInBackground(String... arg0) {
        //your json thread part
    }
    
    @Override
    protected void onPostExecute(final String result) {
            progressDialog.dismiss();
    }
    }
    
    0 讨论(0)
提交回复
热议问题