How do I call the AsyncTask class for another class?

I tried to call the "AsyncTask" class from another "MainActivity" class, but the "AsyncTask" class is inside the "SiteAdapter" class. I tried to pass the link but it doesn't work. How can I do that?

Primary activity:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("StackSites", "OnCreate()");
        setContentView(R.layout.activity_main);

        //Call the class AsyncTask
        new GetAddressTask(this).execute(); // <----ERROR - GetAddressTask cannot be resolved to a type
    }
}

      

AsyncTask inside SitesAdapter classes:

public class SitesAdapter extends ArrayAdapter<StackSite> { 
    ...//PROCESS

    public class GetAddressTask extends AsyncTask<String, Void, String> {
        Context mContext;
        public GetAddressTask(Context context) {
            super();
            mContext = context;
        }

        //Pass a reference to MainActivity
        private MainActivity mainActivity; // <--- WARNING - The value of the field SitesAdapter.GetAddressTask.mainActivity is not used            
        public GetAddressTask(MainActivity mainActivity)
        {
            this.mainActivity = mainActivity;
        }                           

        @Override
        protected String doInBackground(String... arg0) {
            ...         
        }

        @Override
        protected void onPostExecute(String result) {           
            ...
        }
    }
}

      

+3


source to share


4 answers


First of all, MainActivity

it is not a class from which an object cannot be determined. It expands Activity

.

Change your class AsyncTask

,

public static class GetAddressTask extends AsyncTask<String, Void, String> {
    Context mContext;
    public GetAddressTask(Context context) {
         super();
         mContext = context;
    }                          

    @Override
    protected String doInBackground(String... values) {
        // If you want to use 'values' string in here
        String values = values[0];        
    }

    @Override
    protected void onPostExecute(String result) {           
        ...
    }
}

      



Then call it with

String values = ""; //You can pass any string value to GetAddressTask with that parameter
//Call the class AsyncTask
new SitesAdapter.GetAddressTask(this).execute(values);

      

+4


source


make GetAddressTask

static:



public static class GetAddressTask

      

+3


source


Try it, it works for me.

Just create a method in your class SitesAdapter

and call it from MainActivity

like this:

new SitesAdapter().start(MainActivity.this);

      

now in your class SitesAdapter

do this:

private Context mContext;
public void start(){
   mContext = context;
   new GetAddressTask().execute();
}

      

May this help you

+2


source


You can do this static

, then call it with

SitesAdapter.GetAddressTask myClass = new SitesAdapter.GetAddressTask();
myClass.execute();

      

But, if you need it in a few steps, then it is probably worth taking it from this class and putting it in your own file.

If you need to update your UI from a task, you can see this response when used interface

with a callback to your call Activity

.

0


source







All Articles