如何在不同的类中使用databasehelper类中的AsyncTask类工作 [英] How to use databasehelper class in an asynctask class working on a different class
问题描述
大家好我是停留在一个点上,问题是,我有如下三个班,我想实例化的AsyncTask类我DatabaseHelper类。能否请你帮忙,我怎么能在AsyncTask的班级获得上下文?
问题解决了
-
MainActivity类
公共类MainActivity延伸活动{ ... FetchData fetchData =新FetchData(); fetchData.execute(); ... }
-
DatabaseHelper
公共类DatabaseHelper扩展SQLiteOpenHelper { .... 公共DatabaseHelper(上下文的背景下){ 超(背景下,DATABASE_NAME,空,DATABASE_VERSION); .... }
-
FetchData类
公共类FetchData扩展的AsyncTask<字符串,字符串,字符串> { .... DatabaseHelper DB =新DatabaseHelper(); //需要上下文这里! .... }
由于Kasra,我创建了一个fourh类和调用的AsyncTask之前使用它在MainActivity
<醇开始=4>ContextStatic类
公共类ContextStatic {
私有静态语境mContext;
公共静态上下文getmContext(){
返回mContext;
}
公共静态无效setmContext(上下文mContext){
ContextStatic.mContext = mContext;
}
}
更新MainActivity类
公共类MainActivity延伸活动{
...
ContextStatic.setmContext(本);
FetchData fetchData =新FetchData();
fetchData.execute();
...
}
试试这个:
私有类FetchData扩展的AsyncTask&LT;上下文,虚空,虚空&GT; {
保护龙doInBackground(上下文... C){
上下文myContext = C [0];
//做你的东西在这里....
}
保护无效onPostExecute(){
//将您的文章在这里执行code
}
}
您可以拨打这个AsyncTask的由以下行 - 假设你是在一个活动:
新FetchData()执行(这一点)。
如果你不能改变你的AsyncTask的减速,那么你可以尝试使用一个静态变量 - 虽然它是不是有效,pretty的是AsyncTask的减速。试试这个:
类myStatic {
私有静态语境mContext;
静态公共无效setContext(上下文C);
mContext = C;
}
静态公共语境的getContext(){
返回mContext;
}
}
和在你的主code,你叫的AsyncTask之前,调用此:
myStatic.setContext(本);
在你的AsyncTask你doInBackground方法,补充一点:
上下文myContext = myStatic.getContext();
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 please help, how can I get context in AsyncTask class?
Problem Solved
MainActivity class
public class MainActivity extends Activity { ... FetchData fetchData = new FetchData(); fetchData.execute(); ... }
DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper { .... public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); .... }
FetchData class
public class FetchData extends AsyncTask<String, String, String> { .... DatabaseHelper db = new DatabaseHelper(); //need context here!!! .... }
#
Thanks to Kasra, I create a fourh class and use it in MainActivity before calling AsyncTask
ContextStatic class
public class ContextStatic { private static Context mContext; public static Context getmContext() { return mContext; } public static void setmContext(Context mContext) { ContextStatic.mContext = mContext; } }
Updated MainActivity class
public class MainActivity extends Activity {
...
ContextStatic.setmContext(this);
FetchData fetchData = new FetchData();
fetchData.execute();
...
}
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();
这篇关于如何在不同的类中使用databasehelper类中的AsyncTask类工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!