ExpandableListView,已经设置了项目的ArrayList

时间:2017-08-29 22:06:34

标签: android arraylist hashmap expandablelistview expandablelistadapter

我有一个充满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;
}
}

1 个答案:

答案 0 :(得分: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);
    }