【发布时间】:2017-02-06 14:05:31
【问题描述】:
我正在尝试构建一个音乐播放器。 我有一个名为 MusicData 的类,它提供播放列表数据和另一个用于播放列表片段的类,因为我使用的是 viewpager。 播放列表片段使用自定义 expandableListAdapter 类加载数据。
现在的问题是这样的。当用户点击一首歌曲时,它应该被添加到最近播放的播放列表中,这应该反映在播放列表片段中。 基础数据确实发生了变化,因为当我单击播放列表中的第一首歌曲时,它确实播放相同,但包含歌曲标题的视图没有变化
因此,问题本质上是视图不反映基础数据。我试图将 notifyDataSetChanged 方法放在播放列表片段类中,但它不起作用。该怎么办?
这是播放列表片段类的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.playlist, container, false);
expandableListView = (ExpandableListView)v.findViewById(R.id.expandableListViewPlaylist);
Log.e(TAG,"1");
expandableListTitle = new ArrayList<>(MusicData.getExpandableListDetailPlayList().keySet());
expandableListAdapter = new CustomExpandableListAdapter(this.getContext(), expandableListTitle, MusicData.getExpandableListDetailPlayList());
expandableListView.setAdapter(expandableListAdapter);
expandableListAdapter.notifyDataSetChanged();
.......
这是 MusicData 类的代码
public static LinkedHashMap<String, ArrayList<Song>> getExpandableListDetailPlayList(){return expandableListDetailPlayList;}
这是适配器
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> expandableListTitle;
private LinkedHashMap<String, ArrayList<Song>> expandableListDetail;
private String TAG = "AdapterClass";
public CustomExpandableListAdapter(Context context, ArrayList<String> expandableListTitle,
LinkedHashMap<String, ArrayList<Song>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
......
播放列表的数据在 MainActivity 类中更改。那么如何以及在哪里调用 notifyDataSetChanged() 方法呢?
【问题讨论】:
-
请分享处理用户点击的代码部分并相应地将该歌曲添加到列表中
-
在哪里调用 notifyDataSetChanged() 方法?在您的数据发生变化后
标签: android android-fragments expandablelistadapter audio-player notifydatasetchanged android-music-player