我在片段 A 中有一个上下文菜单,其中包含DialogFragment
. 在我的帮助下,DialogFragment
我创建了一个播放列表,它保存在MediaStore.Playlists
. 在片段 B 中,我展示了一个ListView
带有播放列表的内容。
Listview
添加新播放列表时如何在片段 B 中更新?
PS 我m 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();
}
}
这是 TrackFragment,我在其中调用上下文菜单并显示 FragmentDialog:
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