【发布时间】:2013-08-01 09:17:45
【问题描述】:
我有一个方法可以更新 SQLite 数据库并将其放入 AsyncTask 中,以使其更快、更可靠。
但是,更新数据库需要两条数据。一个是整数,另一个是此处显示的 PrimaryKeySmallTank 类的对象。
使用 AsyncTask 的 doInBackground 方法的参数中的 params 数组,我可以传入一个 Integer,但是如果我有两种不同类型的数据呢?
如果整数存储在 int...params[0] 中,我无法在 params[1] 中存储不同类型的对象,那么对此可以做些什么呢?
我想传递给 AsyncTask 的对象
public class PrimaryKeySmallTank {
int contractNumber;
int customerCode;
int septicCode;
String workDate;
int workNumber;
}
我正在使用的 AsyncTask
public class UpdateInfoAsyncTask extends AsyncTask<Integer, Void, Void>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected Void doInBackground(Integer... params) {
Integer mIntegerIn = params[0]; // this is what I want to do, example
PrimaryKeySmallTank mPrimaryKeySmallTank = params[1]; // different data type to pass in
Database db = new Database(InspectionInfoSelectionList.this);
db.openToWrite();
db.updateWorkClassificationByRow(mPrimaryKeySmallTank, mIntegerIn);
db.close();
return null;
}
} // end UpdateInfoAsyncTask
【问题讨论】:
-
你可以在UpdateInfoAsyncTask()构造函数中传递需要的对象。只需为您的异步任务创建一个构造函数
-
doInBackground 方法中的代码是否可以访问作为 UpdateAsyncTask 类的成员变量的数据?就像我可以制作类的成员变量并将数据传递给构造函数一样
-
@Kevik,是的。当然可以。正如您在我的回答中看到的那样,我在
doInBackground方法中写了use intValue。您可以在需要时在该类范围内使用这些成员变量。 :) -
是的。放置在 doInBackground 方法中的代码可以访问传递给构造函数的数据。我添加了一个示例也是我的答案
标签: android multithreading android-asynctask