How to return ArrayList from AsyncTask to another class?

后端 未结 7 683
被撕碎了的回忆
被撕碎了的回忆 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:32

    -- PSEUDO CODE --

    Create a custom interface as followed:

    public interface IAsyncTask {
    
        void IAmFinished(ArrayList arrayList);
    
    }
    
    
    

    Add a constructor to your AsyncTask:

    private IAsyncTask asyncTaskListener;
    
    public MyAsyncTask(IAsyncTask asyncTaskListener){
         this.asyncTaskListener = asyncTaskListener;
    }
    

    In your PostExecute of the AsyncTask:

    public void onPostExecute(List list) {
        asyncTaskListener.IAmFinished(list);
    }
    

    In your Activity that starts your AsyncTask:

    MyAsyncTask asyncTask = new MyAsyncTask(this);
    asyncTask.execute(..);
    

    Implement the interface:

    public class MyActivity implements IAsyncTask
    

    Implement the method:

    public void IAmFinished(ArrayList list){
        // Do whatever you want with your returned object
    }
    
        

    提交回复
    热议问题