OBX
OBX

Reputation: 6114

How to run ProgressDialog in AsyncTask

I am having trouble initializing ProgressDialog in AsyncTask of my Activity, The name of the java file Activity is BigBoard.java .

And here is the java class code of AsyncTask which is inside BigBoard.java:

    class syncX extends AsyncTask<String, String, String>
{
    ProgressDialog progress;

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progress = new ProgressDialog(context); //ERROR
        progress.setMessage("Setting BigBoard ");
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setIndeterminate(true);
        progress.show();
    }

    @Override
    protected String doInBackground(String... params)
    {
        return null;

    }

    @Override
    protected void onPostExecute(String file_url)
    {

    }

}

Now, this is where I am troubled, progress = new ProgressDialog(context);. I tried changing it to :

progress = new ProgressDialog(this);
progress = new ProgressDialog(BigBoard);
progress = new ProgressDialog(BigBoard.this);

But, helpless. How to fix it?

EDIT1

The BigBoard class is provided below as requested.

public class BigBoard extends ActionBarActivity {

ArrayList<String> countryLocal;
String temp;
ArrayAdapter<String> namesArrayAdapter;
ContactInfo ci;
List<ContactInfo> result;
ProgressDialog progress;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Parse.initialize(this, "app-id", "client-key");
    setContentView(R.layout.activity_big_board);
    ci = new ContactInfo();


    RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);
    String abc="hello";

    syncX runner = new syncX();
    runner.execute();




    result = new ArrayList<ContactInfo>();
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Credentials");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> credentialList, ParseException e) {
            if (e == null) {
                for(int i=0;i<credentialList.size();i++)
                {
                    ci.name  = credentialList.get(i).getString("Name");
                    ci.surname = credentialList.get(i).getString("SurName");
                    ci.email = credentialList.get(i).getString("email");
                    result.add(ci);


                    Log.d("OUT", "So the Val::------> " + credentialList.get(i).getString("email"));
                    //result.notifyDataSetChanged();

                }
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });

    ContactAdapter ca = new ContactAdapter(result);
    recList.setAdapter(ca);


}

Upvotes: 1

Views: 47

Answers (1)

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

The progress dialog require to pass the context of your activity. As you suppose to are trying.

progress = new ProgressDialog(context);

Now question is how to pass the context to the progress dialog. Simply the calling of this class. class syncX extends AsyncTask<String, String, String>{} should pass the context to the progress dialog. See the below modify version class.

class syncX extends AsyncTask<String, String, String>
{
        ProgressDialog progress;
        Context mContext;

        public syncX(Context context){
          this.mContext= context;
        }

         public Context getContext(){
           return mContext;
         }


        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            progress = new ProgressDialog(getContext());
            progress.setMessage("Setting BigBoard ");
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setIndeterminate(true);
            progress.show();
        }

        @Override
        protected String doInBackground(String... params)
        {
            return null;
        }

        @Override
        protected void onPostExecute(String file_url)
        {

        }    
}

Inside your BigBoard class it should look like.

new syncX(this).execute();

Done Cheers!

Upvotes: 1

Related Questions