2013-07-16 158 views
3

我犯了一些錯誤,我不知道如何離開。 原來,代碼是這樣的,它的工作原理:在AsyncTask中傳遞ArrayList <HashMap <String,String >>

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


    } 

現在,因爲我把另一個活動,我應該是錯誤的。 學習了一下,我發現你需要使用taskasync,但不知道如何結束。 這是代碼:

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

我該怎麼辦?

非常感謝你

+0

究竟是什麼問題 – Raghunandan

+0

不行,我不知道怎麼的ArrayList傳遞給OnPostExecute。因爲return menuItems;出錯 – Valkyrie00

+0

'doInbackground'計算的結果是'onPostExecute'的參數。改變你的'onpostExecute'參數爲'ArrayList >成功' – Raghunandan

回答

1

只要按照拉古丹丹的建議。

如果你的返回類型是ArrayList < HashMap < 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 [] ..

您的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之外(例如button onclick或onCreate())獲取ArrayList。

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

並且還按照Developer Link For AsyncTask瞭解更多關於它。

萬事如意

相關問題