2014-02-12 70 views
0

如何解決此錯誤類型ArrayList中的方法add(String)不適用於參數HashMap的)。ArrayList <String>類型的方法add(String)不適用於參數(HashMap <String,String>)

我試圖把androidhive.com XML解析自定義列表視圖我的申請

繼承人我的代碼:

private void prepareListData() { 


    //ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 
    ArrayList<String> songsList = new ArrayList<String>(); 


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

    NodeList nl = doc.getElementsByTagName(KEY_SONG); 
    // 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 
     map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
     map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE)); 
     map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST)); 
     map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION)); 
     map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL)); 

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

    listDataHeader = new ArrayList<String>(); 
    listDataChild = new HashMap<String, ArrayList<String>>(); 

    // Adding child data 
    listDataHeader.add("VRP Medical Bay"); 
    //listDataHeader.add(""); 
    //listDataHeader.add(""); 

    // Adding child data 

    ArrayList<String> listUnderVRP = new ArrayList<String>(); 

    //listUnderVRP.add("eDataClinical"); 
    //listUnderVRP.add("Schedule"); 
    //listUnderVRP.add("Dictate"); 
    //listUnderVRP.add("View Messages"); 
    //listUnderVRP.add("Reports for Signature"); 
    //listUnderVRP.add("View Billing"); 
    //listUnderVRP.add("View State"); 


    listDataChild.put(listDataHeader.get(0), songsList); // Header, Child data 
    //listDataChild.put(listDataHeader.get(1), nowShowing); 
    //listDataChild.put(listDataHeader.get(2), comingSoon); 
} 
+0

是什麼錯誤,你得到時,你取消註釋,你聲明數組列表作爲ArrayList的<的HashMap > –

+0

即時獲取此零件的錯誤\t listDataChild.put(listDataHeader.get(0),songsList);方法put(String,ArrayList )在類型HashMap >不適用於參數(String,ArrayList >) – user3243065

回答

0

您的意思是聲明listDataChild = new HashMap<String, ArrayList<String>>();

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

0

我想你應該這樣定義ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();您的ArrayList和它的好,如果你可以發佈您的堆棧跟蹤,這將有助於跟蹤誤差

+0

我不能在這裏發佈圖像,因爲我仍然缺乏點。我的目標是,我有導航抽屜裏面我有可擴展的列表,我jst需要定製我的列表視圖,這就是爲什麼我遵循androidhive.com教程自定義列表視圖,我發現很難實現它在我的導航與展開式應用程序 – user3243065

1

如何修復這個錯誤的Add方法(字符串)類型ArrayList不適用於參數(HashMap)。

如果你想打電話給add(HashMap<String, String>)songsListArrayList,很明顯,你應該daclare它作爲ArrayList<HashMap<String, String>>。否則add方法預計String(因爲您當前的歌曲列表聲明爲ArrayList<String>)。

而且,如果那你想叫putlistDataChildHashMap和傳遞String作爲密鑰,並且您songsListArrayList作爲一種價值,你應該把它聲明爲HashMap<String, ArrayList<HashMap<String, String>>>

換句話說:

private void prepareListData() { 

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

    /* Same old code */ 

    /* 
    * Since listDataChild declared outside this method, do not forget to 
    * replace the old declaration with 
    * HashMap<String, ArrayList<HashMap<String, String>>> listDataChild; 
    */ 
    listDataChild = new HashMap<String, ArrayList<HashMap<String, String>>>(); 

    /* Same old code */ 
} 

注重的意見。

0

到地圖對象添加到ArrayList中,你應該這樣定義如下

ArrayList<HashMap<String, String>>> songsList = new ArrayList<HashMap<String, String>>>; 
songsList.add(map); 
相關問題