0

In my app I am retrieving some data from URL so therefor, I implemented Progress bar in the app by using AsyncTask but the app neither shows the progress bar and nor shows my data. If I call loadDataFromURL() method inside onCreate() method then my data loads but, I have to wait for some time. Please check my code below.

public class MainActivity extends Activity 
{
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = getApplicationContext();

    }

    private void loadDataFromURL() 
    {
        // Here I load some data from URL
    }




    ProgressDialog dialog;

    class LoadData extends AsyncTask<Void, Void, Void> 
    {
        protected void onPreExecute() 
        {
                   dialog = ProgressDialog.show(context, "Loading...", null);
        }

        protected Void doInBackground(Void... unused) 
    {
            try 
            {
                loadDataFromURL();

            Log.d("-DONE", "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
            } 
            catch(Exception e) 
            {
                Log.d("-ERORR", "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
            } 

            return null;
        }

        protected void onProgressUpdate(Void... unused) 
        {
        }

        protected void onPostExecute(Void unused) 
        {
            dialog.dismiss();
        }
    }


}
user2391890
  • 639
  • 1
  • 8
  • 15

3 Answers3

0

You are missing the call for the async task, add

new LoadData().execute();

in your onCreate

Sean
  • 5,176
  • 2
  • 34
  • 50
0

Can you execute or run AsyncTask from our Activity Oncreate.

run AsyncTask via LoadData.execute();

This Method run the AsyncTask .

Thanks

Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
0

Asyntask is loaded on the UI thread. You should call execute to load the asynctask. Alos you add @Override Annotation since its overriden method. You can use the below for reference. Your doInbackground does nothing. So you might not see the dialog since the asynctask finishes its operation very fast. Do some computation so that you can see the dialog.

public class MainActivity extends Activity 
{

@Override 
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dialog = new ProgressDialog(MainActivity.this);
    dialog.setTitle("Loading...");
    new LoadData().execute();  // load asynctsk on ui thread

}

private void loadDataFromURL() 
{
    // Here I load some data from URL
}   

class LoadData extends AsyncTask<Void, Void, Void> 
{
    @Override // missing the override annotation
    protected void onPreExecute() 
    {
        super.onPreExecute() // missing super call
        dialog.show();    // show dialog   
    }
    @Override
    protected Void doInBackground(Void... unused) 
    {   
        try 
        {
            loadDataFromURL(); // do network related operation here

            Log.d("-DONE", "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
            //do not update ui here 
        } 
        catch(Exception e) 
        {
            Log.d("-ERORR", "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
        } 

        return null;
    }
    @Override
    protected void onProgressUpdate(Void... values) {

        super.onProgressUpdate(values); // missing super call
}
    @Override
    protected void onPostExecute(Void result) 
    {
         super.onPostExecute(result) // missing super call
        dialog.dismiss();
    }
}
}

Pls check the link below for more information

http://developer.android.com/reference/android/os/AsyncTask.html

Raghunandan
  • 132,755
  • 26
  • 225
  • 256