How to return ArrayList from AsyncTask to another class?

后端 未结 7 603
被撕碎了的回忆
被撕碎了的回忆 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<Object> 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<String> 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<Object> list){
        // Do whatever you want with your returned object
    }
    
    0 讨论(0)
提交回复
热议问题