我有一種方法可以執行SQLite數據庫更新,並將其放入AsyncTask中以使其更快更可靠。如何將兩種不同的數據類型傳遞給AsyncTask,Android
但是有兩條數據需要更新數據庫。一個是Integer,另一個是此處顯示的PrimaryKeySmallTank類的對象。
在AsyncTask的doInBackground方法的參數中使用params數組,我可以傳遞一個Integer,但是如果我有兩種不同類型的數據,像這樣?
如果整數存儲在int ... params [0]我不能在params [1]中存儲不同的類型對象,那麼可以做些什麼呢?
對象我想通過進入的AsyncTask
public class PrimaryKeySmallTank {
int contractNumber;
int customerCode;
int septicCode;
String workDate;
int workNumber;
}
,我使用
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
@Kevik,是的。當然可以。正如你在我的回答中所看到的,我已經在'doInBackground'方法中寫了'使用intValue'。您可以在該類作用域內使用那些成員變量。 :) –