-2

我有一個ArrayList完整的Song對象,我試圖將它轉換成一個HashMap使用他們的Album名稱作爲關鍵。可悲的是,每個組的父母都會顯示所有的歌曲,每個教程都建議分割數組列表以便按組父項對項目進行分組,所以我能做些什麼? 這裏是我的代碼: AlbumFragment.javaExpandableListView與已經設置的項目ArrayList

public void showAlbums(){ 
    mRootView = mInflater.inflate(R.layout.fragment_album, mContainer, false); 
    mListView = (ExpandableListView) mRootView.findViewById(R.id.listview_album); 
    SongLoader sl = new SongLoader(mContext); 
    final ArrayList<Song> mSongsList = sl.loadSongs(); 
    HashMap<String, List<Song>> mHashSongs = new HashMap<String, List<Song>>(); 

    for(Song song : mSongsList){ 
      mHashSongs.put(song.songAlbum, mSongsList); //I guess the main problem is just here 
    } 

    final ExpandableAlbumListAdapter mAdapter = new ExpandableAlbumListAdapter(getContext(), mAlbumList, mHashSongs); 
    mListView.setAdapter(mAdapter); 
    mListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
     @Override 
     public void onGroupExpand(int groupPosition) { 
      Toast.makeText(getContext(), "List OPENED:"+mAlbumList.get(groupPosition).albumTitle, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

ExpandableAlbumListAdapter.java

public class ExpandableAlbumListAdapter extends BaseExpandableListAdapter { 
private Context mContext; 
private ArrayList<Album> mAlbumList; 
private HashMap<String, List<Song>> mHashChild; 

public ExpandableAlbumListAdapter(Context cx, ArrayList<Album> albumData, HashMap<String, List<Song>> songData){ 
    this.mContext = cx; 
    this.mAlbumList = albumData; 
    this.mHashChild = songData; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    if(convertView == null){ 
     LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.item_album, parent, false); 
    } 

    Album albumView = mAlbumList.get(groupPosition); 

    TextView mAlbumTitle = (TextView) convertView.findViewById(R.id.album_title); 
    TextView mAuthorName = (TextView) convertView.findViewById(R.id.album_artist); 
    TextView mTracksNumb = (TextView) convertView.findViewById(R.id.album_tracks); 
    ImageView mAlbumCover = (ImageView) convertView.findViewById(R.id.album_cover); 

    mAlbumTitle.setText(albumView.albumTitle); 
    mAuthorName.setText(albumView.albumArtist); 
    mTracksNumb.setText(""+albumView.albumNumTracks); 
    ImageLoader.getInstance().displayImage(AppUtils.getAlbumArtUri(albumView.albumID).toString(), mAlbumCover); 

    return convertView; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    final Song childText = (Song) getChild(groupPosition, childPosition); 
    if(convertView == null){ 
     LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.item_album_track, parent, false); 
    } 

    Log.d("test song picked", ""+mHashChild.values()); 
    TextView mSongTitle = (TextView) convertView.findViewById(R.id.album_track_child); 
    mSongTitle.setText(childText.songTitle); 
    return convertView; 
} 

@Override 
public int getGroupCount() { 
    return mAlbumList.size(); 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return this.mHashChild.get(mAlbumList.get(groupPosition).albumTitle).size(); 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return this.mAlbumList.get(groupPosition).albumTitle; 
} 

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    return this.mHashChild.get(this.mAlbumList.get(groupPosition).albumTitle).get(childPosition); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 
} 

回答

1

你的代碼組歌曲的專輯是不正確。您寫道:

final ArrayList<Song> mSongsList = sl.loadSongs(); 
HashMap<String, List<Song>> mHashSongs = new HashMap<String, List<Song>>(); 

for(Song song : mSongsList){ 
     mHashSongs.put(song.songAlbum, mSongsList); 
} 

此代碼將每首歌曲遍歷在mSongsList一個接一個,從歌曲的專輯添加到mHashSongs一個條目mSongsList。機會非常好,你不是故意要put()mSongsList

你可能想要做的是:

  • get()與專輯
  • 如果此列表爲null相關歌曲列表,請在地圖上一個新的,put()
  • add()該歌曲到此列表

代碼:

for (Song song : mSongsList) { 
     List<Song> songsByAlbum = mHashSongs.get(song.songAlbum); 

     if (songsByAlbum == null) { 
      songsByAlbum = new ArrayList<>(); 
      mHashSongs.put(song.songAlbum, songsByAlbum); 
     } 

     songsByAlbum.add(song); 
    } 
+0

工作很好,非常感謝! :) – Sabatino

相關問題