Hi everybody I was stuck at a point, the problem is that I have three classes shown below and I want to instantiate my DatabaseHelper class in AsyncTask class. Could you ple
Try this:
private class FetchData extends AsyncTask<Context, Void, Void> {
protected Long doInBackground(Context... c) {
Context myContext = c[0];
// Do your things here....
}
protected void onPostExecute() {
// Insert your post execute code here
}
}
You can call this AsyncTask by the following line - assuming you are in an activity:
new FetchData().execute(this);
if You cannot change your AsyncTask deceleration, then you can try using a static variable - although it is not as efficient and pretty as AsyncTask deceleration. Try this:
Class myStatic{
private static Context mContext;
static public void setContext(Context c);
mContext = c;
}
static public Context getContext(){
return mContext;
}
}
and in your main code, before you call AsyncTask, call this:
myStatic.setContext(this);
in your doInBackground method of your AsyncTask, add this:
Context myContext = myStatic.getContext();