3

I did a bit of mistakes and I do not know how to get out. Originally, the code is this and it works:

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


    }

Now since I put in another activity, I should be in error. Studying a bit I saw that you need to utlizare the taskasync but do not know how to end. And this is the code:

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

How can I do?

thank you very much

6
  • what exactly is the problem Commented Jul 16, 2013 at 15:39
  • not work, I do not know how to pass arraylist to OnPostExecute. because the return menuItems; goes wrong Commented Jul 16, 2013 at 19:21
  • the result of doInbackground computation is a param to onPostExecute. Change your onpostExecute parm as ArrayList<HashMap<String, String>> success Commented Jul 16, 2013 at 19:25
  • Also change this AsyncTask<ArrayList<String>, Void, ArrayList<HashMap<String, String>>> Commented Jul 16, 2013 at 19:25
  • Since it is a parameter to your doInBackground method, you can also save it as a local member of AsyncTask and later refer to it in your onPostExecute method. Commented Jul 18, 2013 at 15:31

1 Answer 1

2

Just follow the suggestions of Raghunandan.

If your return type is ArrayList < HashMap < String,String > then why you return ArrayList < Sting >.

Change it like:

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

to

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

And Also :

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

to

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

I think your in your program the doInBackground() needs the url as its argument, so you may change it from ArrayList < String > to simple String [] ..

your AsyncTask skeleton is something like:

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

Now you can fetch the ArrayList from outside of AsyncTask(like button onclick or from onCreate() ) like this.

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

And also follow the Developer Link For AsyncTask to know more about it.

All The Best

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.