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

后端 未结 2 1039
你的背包
你的背包 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: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 {
    
    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();
    }
    }
    

提交回复
热议问题