音楽プレーヤーを構築しようとしています。 私はviewpagerを使っているので、PlaylistデータとPlaylistフラグメントのための別のクラスを提供するMusicDataというクラスがあります。 プレイリストフラグメントは、カスタムexpandableListAdapterクラスを使用してデータをロードします。データは変更されましたが、notifyDataSetChangedは機能しません。
これは問題です。ユーザーが曲をクリックすると、最近再生されたプレイリストに追加され、プレイリストの断片に反映されます。 プレイリストから最初の曲をクリックしても同じ再生が行われますが、曲のタイトルを含むビューは変更されないため、基になるデータは変更されます
したがって、基礎データを反映していません。 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();
.......
プレイリストフラグメントクラスのコードだと、これは音楽データのクラスのコード
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()メソッドを呼び出しますか?あなたのデータが変更された後に –