gpt4 book ai didi

java - 异步任务 : pass two or more values from doInBackground to onPostExecute

转载 作者:IT老高 更新时间:2023-10-28 20:31:11 24 4
gpt4 key购买 nike

我有一个 Asynctask,它检索两个 int 值,我想将它们传递给 onPostExecute 以在 View 上显示它们。
这是我的代码:

    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


}

提前谢谢你。

最佳答案

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


}

关于java - 异步任务 : pass two or more values from doInBackground to onPostExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11833978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com