ProgressDialog shows up too late when the task is finished

I am trying to get some data from the database. I want a progressDialog to be shown meanwhile the data is loaded from the database.
And I’m using a class BgClass which extends AsyncTask. On preExecute I show the dialog and on postExecute I dismiss the dialogue.
But still the dialog shows up after the data is loaded from the database, and gets dismissed i.e. doesn’t even show up.
This is the activity that shows the comments to user

public class SeeComments extends AppCompatActivity{


    ListView lv_sc_cmnt;
    ArrayList<String> list;
    static int offset = 0;
    TextView tv_sc_total_reviews;
    int totalReviews = 0;
    final int pageSize = 5;
    String objId,whereClause;
    BackendlessDataQuery dataQuery;
    QueryOptions queryOptions;
    Button btnLoadMore;
    public ProgressDialog dialog;
    Iterator<Rating> iterator;
    ArrayAdapter<String> adapter;
    Rating rating;


    BackendlessCollection<Rating> resAll;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.see_comments);


        lv_sc_cmnt = (ListView) findViewById(R.id.lv_sc_cmnt);
        tv_sc_total_reviews = (TextView) findViewById(R.id.tv_sc_total_reviews);
        list = new ArrayList<>();


        btnLoadMore = new Button(this);
        btnLoadMore.setText("Load More");


        lv_sc_cmnt.addFooterView(btnLoadMore);


        objId = Backendless.UserService.CurrentUser().getObjectId();


        whereClause = "ids_idt like '%_"+objId+"'";
        dataQuery = new BackendlessDataQuery(whereClause);
        queryOptions = new QueryOptions();
        queryOptions.addSortByOption("updated DESC");
        queryOptions.addSortByOption("created DESC");
        dataQuery.setQueryOptions(queryOptions);
        queryOptions.setPageSize(pageSize);


        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);


        lv_sc_cmnt.setAdapter(adapter);


        dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Loading ...");
        dialog.setIndeterminate(false);
        dialog.setCanceledOnTouchOutside(false);


        btnLoadMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addMoreToList();
            }
        });


        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        addMoreToLIst();
    }


    private void addMoreToList() {


        offset+=pageSize;
        dialog.show();


        if (list.size()<totalReviews) {


            BgClass bgClass = new BgClass(resAll,pageSize,offset,this);


            try {
                resAll = bgClass.execute(resAll, null, null).get();
            }catch (InterruptedException e){
                Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
            }catch (ExecutionException e){
                Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
            }


            Iterator&lt;Rating&gt; iterator = resAll.getCurrentPage().iterator();


            while (iterator.hasNext() && list.size()!=totalReviews){
                rating = iterator.next();
                list.add(rating.getComment());
            }
            adapter.notifyDataSetChanged();
            dialog.dismiss();
        } else {
            dialog.dismiss();
            Toast.makeText(this, "All reviews loaded", Toast.LENGTH_LONG).show();
        }
    }
}

And this is my BgClass

public class BgClass extends AsyncTask&lt;BackendlessCollection&lt;Rating&gt;,Integer,BackendlessCollection&lt;Rating&gt;>{


    BackendlessCollection&lt;Rating&gt; allRatings;
    int pageSize,offset;
    ProgressDialog dialog;


    BgClass(BackendlessCollection&lt;Rating&gt; allRatings, int pageSize, int offset, Context context){
        this.allRatings = allRatings;
        this.pageSize = pageSize;
        this.offset = offset;


        dialog = new ProgressDialog(context);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Loading ...");
        dialog.setIndeterminate(true);
        dialog.setCanceledOnTouchOutside(false);
    }


    @Override
    protected BackendlessCollection&lt;Rating&gt; doInBackground(BackendlessCollection&lt;Rating&gt;... params) {


        allRatings = allRatings.getPage(pageSize,offset);
        return allRatings;
    }


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


    }


    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        dialog.setMessage("Loaded "+values);
    }


    @Override
    protected void onPostExecute(BackendlessCollection&lt;Rating&gt; ratingBackendlessCollection) {
        super.onPostExecute(ratingBackendlessCollection);
        dialog.dismiss();
    }
}

Comments are loaded, but ProgressDialog doesn’t show up during the time comments are loaded.

If it is not necessary , Trust me , leave it , Because I tried doing it on my own , It is really tough…
Elias

Hello Elias
Isn’t there any other procedure I can follow to perform this task?

Hi Himanshu

Sorry, but according to Backendless Support Policy, your question is not covered by free support.

Regards, Ilya

Okay, Thank You