【问题标题】:ExpandableListView with an already set ArrayList of itemsExpandableListView 具有已设置的 ArrayList 项目
【发布时间】:2018-02-07 11:51:48
【问题描述】:

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

【问题讨论】:

    标签: android arraylist hashmap expandablelistview expandablelistadapter


    【解决方案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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 2019-01-08
      • 2018-01-04
      • 2015-04-06
      相关资源
      最近更新 更多