我有ArrayList
HashMap
这样:
ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String, String>>();
我填充列表如下:
for (int i1 = 0; i1 < nl.getLength(); i1++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i1);
// adding each child node to HashMap key => value
map.put(KEY_SONGS, String.valueOf(i1));
map.put(KEY_FILE, parser.getValue(e, KEY_FILE));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_FILE, parser.getValue(e, KEY_FILE));
map.put(KEY_ALBUM, parser.getValue(e, KEY_ALBUM));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_ARTWORK, parser.getValue(e, KEY_ARTWORK));
// Songs List of selected playlist
playListSongs.add(map);
// list view bind
PopulateList(playListSongs);
}
现在我想从HashMap-array-list(playListSongs)获取专辑和特定艺术家和歌曲的歌曲数,以将其绑定到列表视图。
我怎样才能做到这一点?
答案 0 :(得分:2)
您可以使用此方法获取特定艺术家的歌曲:
private ArrayList<String> getSongsByArtist(String artist){
ArrayList<String> songs = new ArrayList<String>();
for (HashMap<String, String> hashMap : playListSongs) {
if(hashMap.get(KEY_ARTIST).equals(artist)){
songs.add(hashMap.get(KEY_TITLE));
}
}
return songs;
}
要让不同的艺术家使用以下方法:
private ArrayList<String> getDistinctArtists(){
HashMap<String,String> duplicateTracker = new HashMap<String, String>();
ArrayList<String> distinctArtists = new ArrayList<String>();
for (HashMap<String, String> hashMap : playListSongs) {
// Check whether artist is already added or not
if(duplicateTracker.containsKey(hashMap.get(KEY_ARTIST))==false){
//Add artist name to hash map
duplicateTracker.put(hashMap.get(KEY_ARTIST), hashMap.get(KEY_ARTIST));
distinctArtists.add(hashMap.get(KEY_ARTIST));
}
}
return distinctArtists;
}
答案 1 :(得分:1)
试试这个
for (int i1 = 0; i1 < playListSongs.size(); i1++) {
HashMap<String, String> map = playListSongs.get(i1);
String artist=map.get(KEY_ARTIST);
String songs= map.get(KEY_SONGS);
String file= map.get(KEY_FILE);
String duration= map.get(KEY_DURATION);
SongDescription description=new SongDescription(artists,songs,file,duration);
desList.add(description);
if(artists.equals(desiredAtrist)){
songs----------->You need
}
}