Himanshu
Himanshu

Reputation: 11

android - 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.

public class BgClass extends AsyncTask<parameters>{

ProgressDialog dialog;

BgClass(Context context){

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

@Override
protected doInBackground(parameter) {

    //fetching entries from DB
}

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

}

@Override
protected void onPostExecute(parameter) {
    super.onPostExecute(ratingBackendlessCollection);
    dialog.dismiss();
}

}

ps - I've called the constructor correctly, and execute also with the same object.

This is the code of callingActivity or java file that calls BgClass object(Some irrelevant code removed);

public class SeeComments extends AppCompatActivity{

//variable initialisation

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

    list = new ArrayList<>();

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

    lv_sc_cmnt.addFooterView(btnLoadMore);

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

    lv_sc_cmnt.setAdapter(adapter);

    context = this;

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

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

private void addMoreToList() {

    offset+=pageSize;

    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();
        }

       // add loaded data to list

        adapter.notifyDataSetChanged();
    } else {
        Toast.makeText(this, "All reviews loaded", Toast.LENGTH_LONG).show();
    }
}

}

Upvotes: 0

Views: 245

Answers (2)

Zach
Zach

Reputation: 1964

Move your progress dialog to the onPreExecute

And use getActivity() for the context

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(getActivity());
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.setMessage("Loading ...");
    dialog.setIndeterminate(true);
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
}

Upvotes: 0

sumandas
sumandas

Reputation: 565

You have to have the onProgressUpdate overridden also:

 @Override
 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 setProgressPercent(int value) {
    dialog.setMessage("Loaded value " + value);
 }

Reference : Android Dev site

Upvotes: 1

Related Questions