Asynctask: pass two or more values from doInBackground to onPostExecute

后端 未结 5 1315
无人及你
无人及你 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:53

    public class QueryServer extends AsyncTask {...}
    

    Replace the Integer generic param to ArrayList or to Integer[]

    This way your doInBackground() will look like this:

    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;
    }
    

    And the in your onPostExecute()

    protected void onPostExecute(Integer [] Onlineplayers)
    {
        //Do whatever you want with your array
    }
    

提交回复
热议问题