gpt4 book ai didi

android - 如何将两种不同的数据类型传递给 AsyncTask,Android

转载 作者:可可西里 更新时间:2023-11-01 18:49:28 25 4
gpt4 key购买 nike

我有一个方法可以执行 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

最佳答案

您应该为此创建一个构造函数。

public class UpdateInfoAsyncTask extends AsyncTask<Void, Void, Void>{
int intValue;
String strValue;

public UpdateInfoAsyncTask(int intValue,String strValue){
this.intValue = intValue;
this.strValue = strValue;
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub

}

@Override
protected Void doInBackground(Void... params) {
//use intValue
//use strValue
return null;
}
}

使用它

new UpdateInfoAsyncTask(10,"hi").execute();

关于android - 如何将两种不同的数据类型传递给 AsyncTask,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17989914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com