Asynctask:将两个或多个值从doInBackground传递给onPostExecute

时间:2012-08-06 18:41:25

标签: java android android-asynctask

我有一个Asynctask,它检索两个int值,我也希望在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


    }

提前谢谢。

5 个答案:

答案 0 :(得分:58)

您可以定义一个包含两个整数的Wrapper类:

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


    }

答案 1 :(得分: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
}

答案 2 :(得分: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 :(得分:0)

你有没有试过让这两个成员QueryServer成员?

onPostExecute()doInBackground()完成后才会运行,因此不应出现任何线程问题。

答案 4 :(得分:0)

一般来说,将Integer替换为ArrayList of Objects。使用此数组,您可以传递任何类型的任何集合。但显然,缺点是你应该施展成员,你应该知道所有的类型......