を含むフラグメント A にコンテキスト メニューがありますDialogFragment
。の助けを借りて、DialogFragment
に保存するプレイリストを作成しましたMediaStore.Playlists
。フラグメント B ではListView
、プレイリストを表示しています。
Listview
新しいプレイリストを追加するときにフラグメント Bを更新するにはどうすればよいですか?
PSm trying to use
アダプターで notifydatasetchanged()` を使用していますが、機能しません。
これは、更新できない PlaylistFragment です。
private ListView lvPlaylists;
private int[] images = new int[] { R.drawable.icon_playlist };
private ArrayList<HashMap<String, String>> listPlaylists = new ArrayList<HashMap<String, String>>();;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.playlists, null);
listPlaylists = getAllPlaylists(getActivity());
lvPlaylists = (ListView) v.findViewById(R.id.lvPlaylists);
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
for (int i = 0; i < listPlaylists.size(); i++) {
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("elementPlaylist",
listPlaylists.get(i).get("playlistName"));
datum.put("imagesPlaylist", Integer.toString(images[0]));
data.add(datum);
}
SimpleAdapter sAdapter = new SimpleAdapter(getActivity(), data,
R.layout.list_playlist_element, new String[] {
"elementPlaylist", "imagesPlaylist" }, new int[] {
R.id.tvName, R.id.ivIcon });
lvPlaylists.setAdapter(sAdapter);
lvPlaylists.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
String playlistId = listPlaylists.get(pos).get("playlistId");
Intent playlistSongs = new Intent(getActivity(),
PlaylistSongs.class);
playlistSongs.putExtra("playlistId", playlistId);
startActivity(playlistSongs);
}
});
return v;
}
public ArrayList<HashMap<String, String>> getAllPlaylists(Context context) {
String[] projection = { MediaStore.Audio.Playlists._ID, // 0
MediaStore.Audio.Playlists.NAME // 1
};
Cursor playListSDCardCursor = myquery(context,
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, projection,
null, null, MediaStore.Audio.Playlists.NAME, 0);
if (playListSDCardCursor.moveToFirst()) {
for (int i = 0; i < playListSDCardCursor.getCount(); i++) {
HashMap<String, String> playlist = new HashMap<String, String>();
playlist.put(
"playlistName",
playListSDCardCursor.getString(playListSDCardCursor
.getColumnIndex(MediaStore.Audio.Playlists.NAME)));
playlist.put(
"playlistId",
playListSDCardCursor.getString(playListSDCardCursor
.getColumnIndex(MediaStore.Audio.Playlists._ID)));
listPlaylists.add(playlist);
playListSDCardCursor.moveToNext();
}
}
playListSDCardCursor.close();
return listPlaylists;
}
public static Cursor myquery(Context context, Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder,
int limit) {
try {
ContentResolver resolver = context.getContentResolver();
if (resolver == null) {
return null;
}
if (limit > 0) {
uri = uri.buildUpon().appendQueryParameter("limit", "" + limit)
.build();
}
return resolver.query(uri, projection, selection, selectionArgs,
sortOrder);
} catch (UnsupportedOperationException ex) {
return null;
}
}
これは、新しいプレイリストを作成する DialogFragment です。
public class MyDialogFragment extends DialogFragment {
Context mContext;
public MyDialogFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Введите имя плейлиста:");
final EditText input = new EditText(getActivity());
input.setId(0);
builder.setView(input);
builder.setPositiveButton("Ок", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
Log.d("", "User name: " + value);
if (value != null) {
ContentValues cv = new ContentValues();
cv.put(MediaStore.Audio.Playlists.NAME, value);
Uri uri = getActivity().getContentResolver().insert(
MediaStore.Audio.Playlists
.getContentUri("external"), cv);
}
return;
}
});
builder.setNegativeButton("Отмена",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
}
これは、コンテキスト メニューを呼び出して FragmentDialog を表示する TrackFragment です。
public class TrackFragment extends Fragment {
private ListView lvTracks;
private static ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private int songIndex;
private Utilities utils;
private ArrayList<HashMap<String, String>> listPlaylists = new ArrayList<HashMap<String, String>>();;
private int countPlaylist = 0;
private int itemPosition;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.track, null);
utils = new Utilities();
songsList = getAllSongs(getActivity());
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
for (int i = 0; i < songsList.size(); i++) {
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("songTitle", songsList.get(i).get("songTitle"));
datum.put("songArtist", songsList.get(i).get("songArtist"));
datum.put("songDuration", songsList.get(i).get("songDuration"));
datum.put("songAlbum", songsList.get(i).get("songAlbum"));
data.add(datum);
}
lvTracks = (ListView) v.findViewById(R.id.lvTracks);
lvTracks.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
songIndex = position;
writeData();
Intent mainIntent = new Intent(getActivity().getBaseContext(),
PlayerActivity.class);
mainIntent.putExtra("songIndex", position);
Bundle b = new Bundle();
b.putInt("songIndex", position);
b.putInt("mediaIndex",
Integer.parseInt(songsList.get(position).get("songId")));
mainIntent.putExtras(b);
int albumId = Integer.parseInt(songsList.get(position).get(
"songAlbum"));
Bitmap cover = utils.getAlbumart(albumId, getActivity()
.getApplicationContext());
if (cover == null) {
cover = BitmapFactory.decodeResource(getResources(),
R.drawable.nocover);
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
cover.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
mainIntent.putExtra("albumArt", byteArray);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);
}
});
registerForContextMenu(lvTracks);
SimpleAdapter sAdapter = new SimpleAdapter(getActivity(), data,
R.layout.list_track_element, new String[] { "songTitle",
"songArtist", "songDuration" },
new int[] { R.id.tvSongName, R.id.tvSongArtist,
R.id.tvSongDuration });
lvTracks.setAdapter(sAdapter);
return v;
}
public void onCreateContextMenu(android.view.ContextMenu menu, View v,
android.view.ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
countPlaylist++;
if (countPlaylist > 1) {
listPlaylists.clear();
listPlaylists = getAllPlaylists(getActivity()
.getApplicationContext());
} else {
listPlaylists = getAllPlaylists(getActivity()
.getApplicationContext());
}
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.setHeaderTitle("Добавить в плейлист");
menu.add(0, 0, 0, "Новый плейлист");
for (int i = 0; i < listPlaylists.size(); i++)
menu.add(0, i + 1, 0, listPlaylists.get(i).get("playlistName"));
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
itemPosition = info.position;
if (item.getItemId() == 0) {
createPlaylist(item.getItemId());
}
else {
Log.d("Playlist id",
Integer.parseInt(listPlaylists.get(item.getItemId()).get(
"playlistId"))
+ "");
addToPlaylist(Integer.valueOf(listPlaylists.get(
item.getItemId() - 1).get("playlistId")));
}
return true;
}
private void createPlaylist(int itemId) {
new MyDialogFragment().show(getFragmentManager(), "MyDialog");
}
private void addToPlaylist(int playlistId) {
ContentResolver resolver = getActivity().getContentResolver();
String[] cols = new String[] { "count(*)" };
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
playlistId);
Cursor cur = resolver.query(uri, cols, null, null, null);
cur.moveToFirst();
final int base = cur.getInt(0);
cur.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER,
Integer.valueOf(base + 1));
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID,
Integer.valueOf(songsList.get(itemPosition).get("songId")));
Log.d("mediaIndex", songsList.get(itemPosition).get("songId") + "");
resolver.insert(uri, values);
}
public ArrayList<HashMap<String, String>> getAllPlaylists(Context context) {
String[] projection = { MediaStore.Audio.Playlists._ID,
MediaStore.Audio.Playlists.NAME };
Cursor playListSDCardCursor = myquery(context,
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, projection,
null, null, MediaStore.Audio.Playlists.NAME, 0);
if (playListSDCardCursor.moveToFirst()) {
for (int i = 0; i < playListSDCardCursor.getCount(); i++) {
HashMap<String, String> playlist = new HashMap<String, String>();
playlist.put(
"playlistName",
playListSDCardCursor.getString(playListSDCardCursor
.getColumnIndex(MediaStore.Audio.Playlists.NAME)));
playlist.put(
"playlistId",
playListSDCardCursor.getString(playListSDCardCursor
.getColumnIndex(MediaStore.Audio.Playlists._ID)));
listPlaylists.add(playlist);
playListSDCardCursor.moveToNext();
}
}
playListSDCardCursor.close();
return listPlaylists;
}
}
プレイリストを手動で追加する問題を部分的に解決しました:
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("elementPlaylist", value);
datum.put("imagesPlaylist", Integer.toString(images[0]));
PlaylistFragment.data.add(datum);
プレイリストはリストビューに追加されましたが、クリックするとスローされます:
java.lang.IndexOutOfBoundsException: Invalid index 18, size is 18