"how to use databasehelper class in an asynctask class working on a different class" Code Answer

2

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();
By Nishanthi Grashia on September 4 2022

Answers related to “how to use databasehelper class in an asynctask class working on a different class”

Only authorized users can answer the Search term. Please sign in first, or register a free account.