-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;
}
}
工作很好,非常感謝! :) – Sabatino