传递ArrayList <hashmap <string,string =“”>&gt;在AsyncTask中</hashmap <string,>

时间:2013-07-16 15:29:30

标签: android android-asynctask

我犯了一些错误,我不知道如何离开。 最初,代码就是这样,它起作用了:

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);


    }

现在,因为我进行了另一项活动,我应该是错误的。 学习了一下,我看到你需要完成任务同步,但不知道如何结束。 这就是代码:

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;
        }
    }

我该怎么办?

非常感谢

1 个答案:

答案 0 :(得分:1)

请遵循Raghunandan的建议。

如果您的返回类型是ArrayList&lt; HashMap&lt;字符串,字符串&gt;那么为什么你返回ArrayList&lt; Sting&gt;。

将其更改为:

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&lt;更改它。字符串&gt;简单的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了解更多信息。

所有最佳