使用AsyncTasks作为单独的公共类

时间:2012-03-14 13:24:57

标签: android android-asynctask

这个问题是我所提出的here

的延续

我正在尝试找到一种更好的方法将ArrayList从AsyncTask传递给Activity(任何调用AsyncTask的活动),所以在我的情况下我有SongsManager.java扩展到AsyncTask,如下所示。

我的问题是,它没有归还songsList我的大小为0 ...知道我缺少什么或者我需要进一步调整或添加任何代码吗?

SongsManager.java

 public class SongsManager extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>    {

              public interface SongsMasterCallback {         
            void showSongList(List<HashMap<String, String>> result);     
        } 

        private SongsMasterCallback  mCallback;      
        public SongsManager (SongsMasterCallback  callback) 
        {         
            mCallback = callback;    
        }   

        // Constructor
        //public SongsManager(){ }


        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
        @Override
        protected ArrayList<HashMap<String, String>>  doInBackground(Void... params) 
              {
                 //populating all the data....
           HashMap<String, String> map = new HashMap<String, String>();
                 //.........
                 songsList.add(map);
        }

           return songsList;
        }

        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            // TODO Auto-generated method stub
            //super.onPostExecute(result);
            mCallback.showSongList(result); 
        } 
    }

main.java

public class Main extends Activity implements SongsMasterCallback {

public void showSongList(List<HashMap<String, String>> result) 
{
     this.songsList = (ArrayList<HashMap<String, String>>) result;     
     // then do something with the list here 
}

@Override
public void onCreate(Bundle savedInstanceState) {

  new SongsManager(this).execute();
  Log.d("songsSize", "string : "+songsList.size()); //it shows 0 ??? 

}

}

2 个答案:

答案 0 :(得分:0)

试试这个

public class SongsManager extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>    {

            public interface SongsMasterCallback {         
            void showSongList(List<HashMap<String, String>> result);     
        } 

        private SongsMasterCallback  mCallback;      
        public SongsManager (SongsMasterCallback  callback) 
        {         
            mCallback = callback;    
        }   

        // Constructor
        //public SongsManager(){ }


        @Override
        protected ArrayList<HashMap<String, String>>  doInBackground(Void... params) 
              {
                 ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
                 //populating all the data....
                 HashMap<String, String> map = new HashMap<String, String>();
                 //.........

                 return map;
               }

        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            // TODO Auto-generated method stub
            //super.onPostExecute(result);
            songsList.add(result);
            mCallback.showSongList(result); 
        } 

这是因为你在UI线程中创建songsList的对象并在后台线程中添加数据。

答案 1 :(得分:0)

如果你将songsList的引用传递给AsynTask?

public class SongsManager extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>    {

    private ArrayList<HashMap<String, String>> songsList; 


    public SongsManager (ArrayList<HashMap<String, String>> songsList) 
    {         
       this.songsList = songsList;    
    }   


    @Override
    protected ArrayList<HashMap<String, String>>  doInBackground(Void... params) 
          {
             //populating all the data....
       HashMap<String, String> map = new HashMap<String, String>();
             //.........
             songsList.add(map);
    }

       return songsList;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        // TODO Auto-generated method stub
        //super.onPostExecute(result);

    } 
}

并在main.java中调用 new SongsManager(songsList).execute();