2013-08-01 60 views
13

我有一種方法可以執行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 
+0

您可以在UpdateInfoAsyncTask()構造函數中傳遞所需的對象。只需爲異步任務創建一個構造函數 –

+0

放置在doInBackground方法中的代碼是否可以訪問UpdateAsyncTask類的成員變量?就像我可以創建類的成員變量並將數據傳入該構造函數中的一樣, – Kevik

+0

@Kevik,是的。當然可以。正如你在我的回答中所看到的,我已經在'doInBackground'方法中寫了'使用intValue'。您可以在該類作用域內使用那些成員變量。 :) –

回答

41

您應該創建一個構造函數的AsyncTask。

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(); 
+2

我在我的項目中試過這個,並且它正在工作FANTASTIC – Kevik

+0

我給出了兩個參數的例子,但你可以通過儘可能多的你想要的。很高興幫助:) –

+0

@ChintanRathod需要你在這個幫助http://stackoverflow.com/questions/18054704/how-to-use-scroll-view-in-android – Developer

6

只是傳遞所需的對象在你的異步任務的構造函數,並隨後將在doinBackground()。你可以創建構造該對象,並通過你的對象以下列方式:

public class MyAsyncTask extends AsyncTask<Void, Void, Void>{ 

    PrimaryKeySmallTank tankObj; 

    public UpdateInfoAsyncTask(PrimaryKeySmallTank obj){ 
     tankObj=obj; 
    } 

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

    @Override 
    protected Void doInBackground(Void... params) { 
     //code and use tankObj 
     return null; 
    } 
} 

在您的代碼傳遞所需的對象:

new myAsyncTask(new PrimaryKeySmallTank (1,1,1,"hi",1).execute(); 
4

你也可以嘗試這樣的事:

private class xyz extends AsyncTask<Object, Void, String> { 

@Override 
protected String doInBackground(Object... objects) { 

Long abc = (Long)objects[0]; 
String pqr = (String)objects[1]; 
. 
. 
. 

只是一個簡單的一句話。這樣就應該指出,你不能傳遞原始數據(因爲原語不能作爲Object存儲)。對於原始數據,唯一的方法是像往常一樣轉換包裝(如整數,布爾等)。例如,

int i = (Integer) objects[0]; 
相關問題