gpt4 book ai didi

android - 在 AsyncTask 中传递 ArrayList>

转载 作者:太空狗 更新时间:2023-10-29 14:20:19 26 4
gpt4 key购买 nike

我犯了一些错误,我不知道如何走出去。最初,代码是这样的,它可以工作:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
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
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, parser.getValue(e, KEY_COST));

// adding HashList to ArrayList
menuItems.add(map);
}

// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_COST }, new int[] {
R.id.name, R.id.cost });

setListAdapter(adapter);


}

既然我投入了另一个 Activity ,我应该是错误的。研究了一下我发现你需要使用任务同步但不知道如何结束。这是代码:

public class TaskAsincrono  extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
@Override
protected void onPreExecute(){
}

@Override
protected void onProgressUpdate(Void[] values) {

};

@Override
protected ArrayList<String> doInBackground(ArrayList<String>... params) {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
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
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, parser.getValue(e, KEY_COST));

// adding HashList to ArrayList
menuItems.add(map);

}

return menuItems;
}

@Override
protected void onPostExecute(ArrayList<String> success) {
//se l'alert è visibile viene rimosso
if(dialog.isShowing()) dialog.dismiss();

ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_COST }, new int[] {
R.id.name, R.id.cost });

setListAdapter(adapter);

}

@Override
protected void onCancelled() {
mAuthTask = null;
}
}

我该怎么办?

非常感谢

最佳答案

只需遵循 Raghunandan 的建议即可。

如果你的返回类型是 ArrayList < HashMap < String,String > 那你为什么要返回 ArrayList < Sting >。

改成这样:

public class TaskAsincrono  extends AsyncTask<ArrayList<String>, Void, ArrayList<String>>

public class TaskAsincrono  extends AsyncTask<String, Void, ArrayList<HashMap<String,String>>>

还有:

 @Override
protected ArrayList<String> doInBackground(ArrayList<String>... params)

    @Override
protected ArrayList<HashMap<String,String>> doInBackground(String... params)

我认为您的程序中的 doInBackground() 需要 url 作为其参数,因此您可以将其从 ArrayList < String > 更改为简单的 String [] ..

你的 AsyncTask 骨架是这样的:

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

@Override,
protected void onPreExecute(){
//what you want to do before AsyncTask execution(runs on UI thread)
......................
}

@Override
protected void onProgressUpdate(Void[] values) {
//About updates..........
};

@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

for (String extraUrl : params) {
//do all stuffs here
//like parse xml stuffs
//put them to hashmap then add the hashmap to arraylist(menuitems)....
}
return menuItems;
}
}

现在您可以像这样从 AsyncTask 外部获取 ArrayList(如按钮 onclick 或从 onCreate() )。

 TaskAsincrono  task = new TaskAsincrono  ();
try {
ArrayList<HashMap<String, String>> arr = new ArrayList<HashMap<String, String>>();
arr = task.execute(URL).get();

同时也关注Developer Link For AsyncTask了解更多。

一切顺利

关于android - 在 AsyncTask 中传递 ArrayList<HashMap<String, String>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com