-3

Progress dialog should appear before display Alert dialog in Android app . I am using android studio.

Alert dialog content will be from Async task in separate class file. So excuting Progress dialog from async task.

But i am not able to see progress dialog screen before AlertDialog opens.

here is my async task code below.

  public class ResidentsPaymentInfoHttpResponse extends AsyncTask<String, 
Void, List<paymentInfo>> {
ProgressDialog pDialog;
private Context MSAContext;
public ResidentsPaymentInfoHttpResponse(Context context)   {
 MSAContext = context;
}

   @Override
    protected void onPreExecute() {
    super.onPreExecute();

    pDialog = ProgressDialog.show(MSAContext,"Autenticando", "Contactando o 
   servidor, por favor, aguarde alguns instantes.", true, false);
   }

 @Override
  protected List<UserPaymentInfo> doInBackground(String... params){
  String flatNo = params[0];
  String urls = "https://script.google.com/macros/s/;"
  List<UserPaymentInfo> residentsMonthlyPayments = new ArrayList<>();

  try {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(urls)
            .build();

    Response responses = null;
    try
    {
        responses = client.newCall(request).execute();
        String jsonData = responses.body().string();
        JSONObject jobject = new JSONObject(jsonData);
        JSONArray jarray = jobject.getJSONArray("ResidentsInfo");

        int limit = jarray.length();

        for(int i=0;i<limit; i++)
        {
            JSONObject object = jarray.getJSONObject(i);
            if(object.getString("FlatNo").equals(flatNo) && 
      object.getString("PaymentStatus").equals("notpaid")) {
                UserPaymentInfo residentMaintePayment = new 
   UserPaymentInfo();
                UserInfo residentInfo = new UserInfo();
                residentInfo.setUserFlatNo(object.getString("FlatNo"));

                residentsMonthlyPayments.add(residentMaintePayment);
            }
        }

    }

    catch (IOException e)
    {
      //  e.printStackTrace();
    }
    pDialog.dismiss();
}
catch (Exception ex)
{
   // ex.printStackTrace();
}
return residentsMonthlyPayments;
}


protected void onPostExecute(List<UserPaymentInfo> rusult){
    super.onPostExecute(rusult);
    pDialog.dismiss();
 }
 }

Am i missing something???

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
Kumar
  • 21
  • 5

2 Answers2

0

You should not update UI elements (which belong to main/UI thread) inside doInBackground(). May be removing pDialog.dismiss(); from end lines of doInBackground() change the situation.

VSB
  • 9,825
  • 16
  • 72
  • 145
0

Check below link. How to show progress dialog in Android?

you are not calling show() method on progress dialog. You should do it inside preExecute then dismiss it in postExecute method of async task.

Also as said by VSB you should not update UI elements from doInBackground method.

TheGraduateGuy
  • 1,450
  • 1
  • 13
  • 35