Using Asynctasks As Separate Public Classes
this question is a continuation of what i have asked orginally here i am trying to find a better way to pass the ArrayList from AsyncTask to the Activity (any activity that calls
Solution 1:
And if you pass the reference of the songsList to the AsynTask?
publicclassSongsManagerextendsAsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
privateArrayList<HashMap<String, String>> songsList;
publicSongsManager (ArrayList<HashMap<String, String>> songsList)
{
this.songsList = songsList;
}
@OverrideprotectedArrayList<HashMap<String, String>> doInBackground(Void... params)
{
//populating all the data....HashMap<String, String> map = newHashMap<String, String>();
//.........
songsList.add(map);
}
return songsList;
}
@OverrideprotectedvoidonPostExecute(ArrayList<HashMap<String, String>> result) {
// TODO Auto-generated method stub//super.onPostExecute(result);
}
}
and in the main.java call
new SongsManager(songsList).execute();
Solution 2:
Try this
publicclassSongsManagerextendsAsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
publicinterfaceSongsMasterCallback {
voidshowSongList(List<HashMap<String, String>> result);
}
privateSongsMasterCallback mCallback;
publicSongsManager (SongsMasterCallback callback)
{
mCallback = callback;
}
// Constructor//public SongsManager(){ }@OverrideprotectedArrayList<HashMap<String, String>> doInBackground(Void... params)
{
ArrayList<HashMap<String, String>> songsList = newArrayList<HashMap<String, String>>();
//populating all the data....HashMap<String, String> map = newHashMap<String, String>();
//.........return map;
}
@OverrideprotectedvoidonPostExecute(ArrayList<HashMap<String, String>> result) {
// TODO Auto-generated method stub//super.onPostExecute(result);
songsList.add(result);
mCallback.showSongList(result);
}
This is happening because you are creating object of songsList in UI thread and adding data in background thread.
Post a Comment for "Using Asynctasks As Separate Public Classes"