Asynctask: pass two or more values from doInBackground to onPostExecute

后端 未结 5 1323
无人及你
无人及你 2020-12-03 01:06

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 Q         


        
5条回答
  •  感情败类
    2020-12-03 01:48

    The most straightforward way is simply declare a small container object with the fields you want to return, then return an instance of that from 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 );
    }
    

提交回复
热议问题