2012-08-06 88 views
22

我有一個Asynctask,它檢索兩個int值,我想將它們傳遞給onPostExecute以在視圖上顯示它們。
這裏是我的代碼:Asynctask:將兩個或更多值從doInBackground傳遞到onPostExecute

public class QueryServer extends AsyncTask <String, Void, Integer> { 

    protected Integer doInBackground(String... serverAddress) { 
     Log.d("QueryServer", ""+serverAddress[0]); 
     MCQuery mcQuery = new MCQuery("" + serverAddress[0] ,25565); 
     QueryResponse response = mcQuery.basicStat(); 

     int Onlineplayers = response.getOnlinePlayers(); //first vaule 
     int Maxplayers = response.getMaxPlayers(); //second vaule 

     Log.d("MCQuery", "" + Onlineplayers + " OnlinePlayers"); 
     return Onlineplayers; 

    } 

    protected void onPostExecute(Integer Onlineplayers){ 

     TextView onlinePlayersView = (TextView) findViewById(R.id.online_players); 

     onlinePlayersView.setText(""+Onlineplayers+"/"+ Maxplayers); //i need to pass Maxplayers to use it here 


    } 

預先感謝您。

回答

54

您可以定義保存兩個整數的包裝類:

public class Wrapper 
{ 
    public int onlinePlayers; 
    public int maxPlayers; 
} 

和到位的Integer使用它:

public class QueryServer extends AsyncTask<String, Void, Wrapper> { 

    protected Wrapper doInBackground(String... serverAddress) { 
     Log.d("QueryServer", ""+serverAddress[0]); 
     MCQuery mcQuery = new MCQuery("" + serverAddress[0] ,25565); 
     QueryResponse response = mcQuery.basicStat(); 

     int onlinePlayers = response.getOnlinePlayers(); //first vaule 
     int maxPlayers = response.getMaxPlayers(); //second vaule 

     Log.d("MCQuery", "" + onlinePlayers + " onlinePlayers"); 
     Wrapper w = new Wrapper(); 
     w.onlinePlayers = onlinePlayers; 
     w.maxPlayers = maxPlayers; 
     return w; 

    } 

    protected void onPostExecute(Wrapper w){ 

     TextView onlinePlayersView = (TextView) findViewById(R.id.online_players); 

     onlinePlayersView.setText(""+w.onlinePlayers+"/"+ w.maxPlayers); //i need to pass Maxplayers to use it here 


    } 
+3

偉大的答案!謝謝 – Harry 2013-02-27 07:18:09

+0

謝謝,簡單而有效 – 2014-01-27 17:49:55

2

最直接的方法是簡單地宣佈用一個小容器對象字段,然後從doInBackground返回一個實例:

private class QueryResult { 
    int onlinePlayers; 
    int maxPlayers; 

    public QueryResult(int onlinePlayers, int maxPlayers) { 
     this.onlinePlayers = onlinePlayers; 
     this.maxPlayers = maxPlayers; 
    } 
} 

protected QueryResult doInBackground(String... serverAddress) { 
    // ... 

    return new QueryResult(onlinePlayers, maxPlayers); 
} 
3
public class QueryServer extends AsyncTask <String, Void, Integer>{...} 

更換Integer通用的參數去ArrayList<Integer>Integer[]

這樣你doInBackground()看起來就像這樣:

protected Integer[] doInBackground(String... serverAddress) 
{ 
    ...do what you need to do... 
    //Then create an array, fill with data, and return. 
    Integer[] arr = new Integer[10]; 
    for(int i=0; i<10; i++) 
     arr[i] = i; //just an example 
    return arr; 
} 

而且在onPostExecute()

protected void onPostExecute(Integer [] Onlineplayers) 
{ 
    //Do whatever you want with your array 
} 
0

您是否嘗試過做QueryServer這兩個int成員?

不會運行,直到doInBackground()完成後,所以不應該有任何線程問題。

0

一般來說,用ArrayList of Objects代替Integer。有了這個數組,你可以傳遞任何類型的集合。但顯然,缺點是你應該投出成員,你應該知道所有類型...

相關問題