How to return ArrayList from AsyncTask to another class?

后端 未结 7 696
被撕碎了的回忆
被撕碎了的回忆 2021-01-21 20:01

i want to get Ftp folders list from server using AsyncTask and return folders names ArrayList to main class and update spinner adapter.

In main class i got spinner with

7条回答
  •  死守一世寂寞
    2021-01-21 20:12

    Implement a listener passing ArrayList and use this listener for returning your ArrayList.

    public interface TaskListener {
        public void onSuccess(ArrayList result);
    
    }
    

    While invoking your async task for operation execution create an instance of TaskListener as follows:

    TaskListener listener = new TaskListener() {
    
            @Override
            public void onSuccess(ArrayList result) {
                   // Your result will come here
            }
        };
    

    Pass this listenerobject as a parameter to the async task constructor. And create a global instance of TaskListener in the async task itself. Assign the TaskListener parameter in the constructor to the global instance.

    Then in the onPostExecute of the async task class:

    protected ArrayList[] onPostExecute(ArrayList... result) {
            this.taskListenerGlobalInstance(result); // this will invoke the call back method 
    
        }
    

提交回复
热议问题