带有已设置项目 ArrayList 的 ExpandableListView

ExpandableListView with an already set ArrayList of items

我有一个包含 Song 个对象的 ArrayList,我尝试使用它们的 Album 名称作为键将其转换为一个 HashMap。可悲的是,每个组父都显示所有歌曲,每个教程都建议划分 arrayList 以便按组父对项目进行分组,那么我该怎么办? 这是我的代码: AlbumFragment.java

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

您按专辑对歌曲进行分组的代码不正确。您写道:

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