基于@Tom解决方案进行更新:
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 ???
}
}
更新结束
我正在使用AsyncTask开发一个类,下面是我尝试执行时得到的内容
错误在设计时间,因此没有LogCat
这是班级:
SongManager.java
public class SongsManager extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
// Constructor
public SongsManager(){ }
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
// TODO Auto-generated method stub
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
Xml_Parser_Custom_Listview parser = new Xml_Parser_Custom_Listview();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_NAME);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
//adding the rows into to the map...
// adding HashList to ArrayList
songsList.add(map);
}
return songsList;
}
}
main.java文件
尝试执行AsyncTask:
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
songsList = new SongsManager().execute();
这是错误:
Type mismatch: cannot convert from AsyncTask<Void,Void,ArrayList<HashMap<String,String>>> to ArrayList<HashMap<String,String>>
答案 0 :(得分:0)
SongsManager.execute()
返回的值不是计算结果;这是AsyncTask
本身。要发布结果,请在SongsManager
中编写方法:
void onPostExecute(ArrayList<HashMap<String,String>> result) {
songsList = result; // or something similar
// then update the UI to reflect the results
}
在此方法中,songsList
是您现在尝试从new SongsManager.execute()
分配返回值的变量。如果SongsManager
类看不到此变量(例如,它不是主类的嵌套类),那么您将不得不安排回调:
public class SongsManager
extends AsyncTask<Void, Void, List<Map<String,String>>
{
public interface SongsMasterCallback {
void showSongList(List<Map<String, String>> result);
}
private SongsMasterCallback mCallback;
public SongsMaster(SongsMasterCallback callback) {
mCallback = callback;
}
@Override
protected List<Map<String, String>> doInBackground(Void... params) {
// as before
}
@Override
protected void onPostExecute(List<Map<String, String>> result) {
mCallback.showSongList(result);
}
}
然后将您的活动声明为implement SongsManager.SongsManagerCallback
并添加方法:
void showSongList(List<Map<String, String>> songList) {
this.songList = songList;
// then do something with the list here
}
答案 1 :(得分:0)
这是因为在创建/执行AsyncTask时没有得到ArrayList<HashMap<String, String>>
。你可以在onPostExecute()上找回它。
您应该像这样创建/执行任务
SongsManager songsList = new SongsManager().execute();
然后像你这样在你的AsyncTask中有一个onPostExecute
protected void onPostExecute(ArrayList<HashMap<String, String>> result)
{
// do stuff with the ArrayList<HashMap<String, String>> result parameter.
}