24

我有一个 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


    }

先感谢您。

4

5 回答 5

59

您可以定义一个包含两个整数的 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


    }
于 2012-08-06T18:45:25.113 回答
4
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
}
于 2012-08-06T18:45:59.077 回答
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 );
}
于 2012-08-06T18:45:45.513 回答
0

您是否尝试过使这两个 ints 成员成为QueryServer

onPostExecute()在 after 完成之前不会运行doInBackground(),因此不应该有任何线程问题。

于 2012-08-06T18:48:43.407 回答
0

一般来说,替换IntegerArrayList of Objects。使用此数组,您可以传递任何类型的任何集合。但很明显,缺点是你应该选派成员,你应该知道所有的类型......

于 2017-07-09T10:39:12.253 回答