Type Mismatch: Cannot Convert From Asynctask
Solution 1:
The value returned from SongsManager.execute()
is not the results of the computation; it's the AsyncTask
itself. To publish your results, write a method in SongsManager
:
voidonPostExecute(ArrayList<HashMap<String,String>> result) {
songsList = result; // or something similar// then update the UI to reflect the results
}
In this method, songsList
is the variable to which you are now trying to assign the return value from new SongsManager.execute()
. If this variable is not visible to the SongsManager
class (e.g., it's not a nested class of your main class), then you'll have to arrange for a call-back:
publicclassSongsManagerextendsAsyncTask<Void, Void, List<Map<String,String>>
{
publicinterfaceSongsMasterCallback {
voidshowSongList(List<Map<String, String>> result);
}
privateSongsMasterCallback mCallback;
publicSongsMaster(SongsMasterCallback callback) {
mCallback = callback;
}
@OverrideprotectedList<Map<String, String>> doInBackground(Void... params) {
// as before
}
@OverrideprotectedvoidonPostExecute(List<Map<String, String>> result) {
mCallback.showSongList(result);
}
}
Then declare your activity to implement SongsManager.SongsManagerCallback
and add is method:
voidshowSongList(List<Map<String, String>> songList) {
this.songList = songList;
// then do something with the list here
}
Solution 2:
That's because you do not get the ArrayList<HashMap<String, String>>
returned when you create / execute the AsyncTask. You get it back on the onPostExecute().
You should create/execute the task like this
SongsManagersongsList=newSongsManager().execute();
and then have a onPostExecute in your AsyncTask like this
protectedvoidonPostExecute(ArrayList<HashMap<String, String>> result)
{
// do stuff with the ArrayList<HashMap<String, String>> result parameter.
}
Post a Comment for "Type Mismatch: Cannot Convert From Asynctask"