Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In Screen have Button. Once Use click on Button an AsyncTask will execute and fetch the data from database and show in the AlertDialog. Between ButtonClick and AlertDialog time needs to show CircularProgressBar.

My code is written below but Progress bar is not showing.

Any other workaround for the requirement.

 public void Select_PaymentMonths(String selectFlatNumber)
   {

    /*
    progressBarLoadData = new ProgressDialog(get ApplicationContext());
    progressBarLoadData.setMessage("Please Wait....");
    progressBarLoadData.show();
    */
  //  ProgressDialog progressDialog = new ProgressDialog(this);
  //  progressDialog.setMessage("Please Wait......");
 //   progressDialog.show();
    //monthProgress.setVisibility(View.VISIBLE);

   //ProgressDialog progressDialog = new ProgressDialog(this);
    //  progressDialog.setMessage("Please Wait......");
    ProgressDialog progressDialog =   ProgressDialog.show(this,"Please Wait","Wait for loading Payment Month data");

    residentsPaymentInfo = new ArrayList<UserPaymentInfo>();
    ResidentsPaymentInfoHttpResponse getResidentsPaymentMonthDetails = new 
     ResidentsPaymentInfoHttpResponse();
    try {
    residentsPaymentInfo = 
            getResidentsPaymentMonthDetails.execute(selectFlatNumber).get();
        //monthProgress.setVisibility(View.GONE);
        progressDialog.dismiss();
        int notPaidMonthsCount = residentsPaymentInfo.size();
        List<String> listItems = new ArrayList<String>();


        for(int i=0; i<notPaidMonthsCount; i++ )
        {
            UserPaymentInfo userNotPaidMonthInfo = new UserPaymentInfo();
            userNotPaidMonthInfo = residentsPaymentInfo.get(i);

            //listItems.add(userNotPaidMonthInfo.getPaymentMonth() +","+ 
           userNotPaidMonthInfo.getPaymentoYear() +" - ?" + 
            userNotPaidMonthInfo.getactualAmount() + "/-");
            listItems.add(userNotPaidMonthInfo.getPaymentMonth() +" "+ 
          userNotPaidMonthInfo.getPaymentoYear() +" ?" + 
            userNotPaidMonthInfo.getactualAmount() + "/-");
        }

     final CharSequence[] items = listItems.toArray(new 
         CharSequence[listItems.size()]);
       // arraylist to keep the selected items
        final ArrayList seletedItems=new ArrayList();
        //progressBarLoadData.dismiss();
        //progressDialog.dismiss();
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("Select Payment Month Year Amount")
                .setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int 
               indexSelected, boolean isChecked) {
                        if (isChecked) {
                            // If the user checked the item, add it to the 
               selected items
                            //seletedItems.add(indexSelected);
                            seletedItems.add(indexSelected);
                        } else if (seletedItems.contains(indexSelected)) {
                            // Else, if the item is already in the array, 
       remove it

    seletedItems.remove(Integer.valueOf(indexSelected));
                        }
                    }
                }).setPositiveButton("OK", new 
      DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        Collections.sort(seletedItems);

                        String selectedMonths = "";
                        float totalAmount=0;
                        for(int i = 0 ; i < seletedItems.size(); i++) {
                            //selectedMonths = selectedMonths + items[(int)
          (seletedItems.get(i))];
                            selectedMonths = selectedMonths +             
  residentsPaymentInfo.get((int)seletedItems.get(i)).getPaymentMonth() +"-
 "+residentsPaymentInfo.get((int)seletedItems.get(i)).getPaymentoYear()+",";
                            totalAmount = totalAmount + 
residentsPaymentInfo.get((int)seletedItems.get(i)).getactualAmount();
                        }

                        paymentMonth.setText(selectedMonths);
                        amountPaid.setText(String.valueOf(totalAmount));
                //selectedMonths.split(",");
                //  Your code when user clicked on OK
                //  You can write the code  to save the selected item here

             }
       }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                  {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //  Your code when user clicked on Cancel
                    }
                }).create();
        //return 50;
        dialog.show();

    }
    catch (Exception e)
    {
     //   e.printStackTrace();
    }

}

I have update the code ProgressDialog code into Async task like below but still progress dialog is not appearing.

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

   @Override
       protected void onPreExecute(){
       pDialog = new ProgressDialog(MSAContext);
       pDialog.setMessage("Loading...");
        pDialog.show();
    }
   @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"));
                residentInfo.setUserName(object.getString("Name"));
                residentInfo.setUserEamil(object.getString("OwnerEmail"));
                residentMaintePayment.setResidentData(residentInfo);
                residentMaintePayment.setactualAmount(object.getLong("Actualamountneedtopay"));
                residentMaintePayment.setPaymentYear(object.getInt("Year"));
                residentMaintePayment.setPaymentMonth(object.getString("Month"));
                residentsMonthlyPayments.add(residentMaintePayment);
            }
        }

    }

    catch (IOException e)
    {
      //  e.printStackTrace();
    }

}
catch (Exception ex)
{
   // ex.printStackTrace();
}
return residentsMonthlyPayments;
}

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


}

}

In Maintask calling async task like below

ResidentsPaymentInfoHttpResponse getResidentsPaymentMonthDetails = new 
   ResidentsPaymentInfoHttpResponse(this);
     try {
        residentsPaymentInfo = 
     getResidentsPaymentMonthDetails.execute(selectFlatNumber).get();
        //monthProgress.setVisibility(View.GONE);
       // progressDialog.dismiss();
         int notPaidMonthsCount = residentsPaymentInfo.size();
         List<String> listItems = new ArrayList<String>();

Progress bar screen not appearing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
132 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is not directly change your code, but give u a example. Put your logical in Thread.runnable, then after finish your work, dismiss ProgressDialog .

http://indyvision.net/2015/08/android-tutorials-show-a-progress-dialog-while-loading-data/

//inside the runnable will be the logic that you want to run
    void showProgressDialog(final Context context, final Runnable runnable) {
        final ProgressDialog ringProgressDialog = ProgressDialog.show(context, "Title ...", "Info ...", true);
        //you usually don't want the user to stop the current process, and this will make sure of that
        ringProgressDialog.setCancelable(false);
        Thread th = new Thread(new Runnable() {
            @Override
            public void run() {

                runnable.run();
                //after he logic is done, close the progress dialog
                ringProgressDialog.dismiss();
            }
        });
        th.start();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...