本文介绍了AsyncTask的:从doInBackground传递两个或多个值onPostExecute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个AsyncTask的以检索两个int vaules,我想通过他们onPostExecute展示他们的看法。结果
这里是我的code:
I have an Asynctask which retrieves two int vaules and i want to pass them to onPostExecute to show them on the view.
Here is my code:
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
}
感谢您提前。
推荐答案
您可以定义包含两个整数包装类:
You can define a Wrapper class that holds two integers:
public class Wrapper
{
public int onlinePlayers;
public int maxPlayers;
}
和在整数
的使用它:
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
}
这篇关于AsyncTask的:从doInBackground传递两个或多个值onPostExecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!