Hello I've been having this problem for months , so please somebody help, I am a beginner in developing apps for android , that's why i wasn't able to fix this.
The Problem: Developing A Music Player, my problem is with the playlists and its id, when i pass the PlaylistId under the arguments its not returning anything so the screen stays white. Now I've tried a lot of stuff to fix this and i managed to get that the problem is with the arguments (The Adapter is fine,Layout is fine,Everything else is fine) So Please I Know Its Gonna Take Some Time And Work Put Please Help.
NavUtils :
public final class NavUtils {
public static void openSearch(final Activity activity, final String query) {
final Bundle bundle = new Bundle();
final Intent intent = new Intent(activity, Search.class);
intent.putExtra(SearchManager.QUERY, query);
intent.putExtras(bundle);
activity.startActivity(intent);
}
/**
* Opens the profile of an artist.
*
* @param context The {@link android.app.Activity} to use.
* @param artistName The name of the artist
*/
public static void openArtistProfile(final Activity context,
final String artistName) {
// Create a new bundle to transfer the artist info
final Bundle bundle = new Bundle();
bundle.putLong(Config.ID, Utils.getIdForArtist(context, artistName));
bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Artists.CONTENT_TYPE);
bundle.putString(Config.ARTIST_NAME, artistName);
// Create the intent to launch the profile activity
final Intent intent = new Intent(context, Profile.class);
intent.putExtras(bundle);
context.startActivity(intent);
}
/**
* Opens the profile of an album.
*
* @param context The {@link android.app.Activity} to use.
* @param albumName The name of the album
* @param artistName The name of the album artist
* @param albumId The id of the album
*/
public static void openAlbumProfile(final Activity context,
final String albumName, final String artistName, final long albumId) {
// Create a new bundle to transfer the album info
final Bundle bundle = new Bundle();
bundle.putString(Config.ALBUM_YEAR, Utils.getReleaseDateForAlbum(context, albumId));
bundle.putString(Config.ARTIST_NAME, artistName);
bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Albums.CONTENT_TYPE);
bundle.putLong(Config.ID, albumId);
bundle.putString(Config.NAME, albumName);
// Create the intent to launch the profile activity
final Intent intent = new Intent(context, Profile.class);
intent.putExtras(bundle);
context.startActivity(intent);
}
public static void openPlaylistProfile(final Activity context,
final String playlistName, final long playlistId) {
// Create a new bundle to transfer the album info
final Bundle bundle = new Bundle();
bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Playlists.CONTENT_TYPE);
bundle.putLong(Config.ID, playlistId);
bundle.putString(Config.NAME, playlistName);
// Create the intent to launch the profile activity
final Intent intent = new Intent(context, Profile.class);
intent.putExtras(bundle);
context.startActivity(intent);
}
}
PlaylistFragment:
public class PlaylistFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>,OnItemClickListener {
private PlaylistsAdapter mAdapter;
GridView gridview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_playlist, container, false);
mAdapter = new PlaylistsAdapter(getActivity(), null);
gridview = (GridView) myFragmentView.findViewById(R.id.playlistGrid);
getLoaderManager().initLoader(0, null, this);
return myFragmentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
gridview.setAdapter(mAdapter);
gridview.setOnItemClickListener(this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
new String[] {
/* 0 */
BaseColumns._ID,
/* 1 */
MediaStore.Audio.PlaylistsColumns.NAME
}, null, null, MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Bundle bundle = new Bundle();
Cursor cursor = mAdapter.getCursor();
NavUtils.openPlaylistProfile(getActivity(),cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.NAME)),cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID)));
}
}
PlaylistSong:
public class PlaylistSong extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>,AdapterView.OnItemClickListener {
private SongAdapter mAdapter;
ListView listView;
long playlistID;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_song, container, false);
mAdapter = new SongAdapter(getActivity(), null);
listView = (ListView) myFragmentView.findViewById(R.id.songlist);
final Bundle arguments = getArguments();
getLoaderManager().initLoader(0, null, this); /// if i set the second parameters as arguments instead of null it returns NullPointer
playlistID = arguments.getLong(Config.ID);
return myFragmentView;
}
/**
* {@inheritDoc}
*/
@Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putAll(getArguments() != null ? getArguments() : new Bundle());
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(this);
listView.setFastScrollEnabled(true);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
final StringBuilder mSelection = new StringBuilder();
mSelection.append(MediaStore.Audio.AudioColumns.IS_MUSIC + "=1");
mSelection.append(" AND " + MediaStore.Audio.AudioColumns.TITLE + " != ''");
return new CursorLoader(getActivity(),MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID),
new String[] {
/* 0 */
MediaStore.Audio.Playlists.Members._ID,
/* 1 */
MediaStore.Audio.Playlists.Members.AUDIO_ID,
/* 2 */
MediaStore.Audio.AudioColumns.TITLE,
/* 3 */
MediaStore.Audio.AudioColumns.ARTIST,
/* 4 */
MediaStore.Audio.AudioColumns.ALBUM,
/* 5 */
MediaStore.Audio.AudioColumns.DURATION
}, mSelection.toString(), null,
MediaStore.Audio.AudioColumns.TITLE);
}
/* if i replace the above code with the one below it displays all the songs fine
String select = null;
final StringBuilder mSelection = new StringBuilder();
mSelection.append(MediaStore.Audio.AudioColumns.IS_MUSIC + "=1");
mSelection.append(" AND " + MediaStore.Audio.AudioColumns.TITLE + " != ''");
return new CursorLoader(getActivity(),MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] {
/* 0 */
BaseColumns._ID,
/* 1 */
MediaStore.Audio.AudioColumns.TITLE,
/* 2 */
MediaStore.Audio.AudioColumns.ARTIST,
/* 3 */
MediaStore.Audio.AudioColumns.ALBUM,
/* 4 */
MediaStore.Audio.AudioColumns.DURATION,
/*5*/
MediaStore.Audio.AudioColumns.ALBUM_ID
}, mSelection.toString(), null,
MediaStore.Audio.AudioColumns.TITLE);
*/
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
Profile:
public class Profile extends FragmentActivity {
/**
* The Bundle to pass into the Fragments
*/
private Bundle mArguments;
public static long playlistId;
/**
* MIME type of the profile
*/
private String mType;
public static long ID;
private long IDLong;
PagerAdapter mPagerAdapter;
/**
* Artist name passed into the class
*/
private String mArtistName;
private String mPlaylistName;
/**
* The main profile title
*/
private String mProfileName;
private Drawable ActionBarBackgroundDrawable;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Temporay until I can work out a nice landscape layout
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_profile);
///TODO FIX PLAYLIST ARGUMENTS
// Initialize the Bundle
mArguments = savedInstanceState != null ? savedInstanceState : getIntent().getExtras();
// Get the MIME type
mType = mArguments.getString(Config.MIME_TYPE);
ID = mArguments.getLong(Config.ID);
// Initialize the pager adapter
mPagerAdapter = new PagerAdapter(this);
// Get the profile title
mProfileName = mArguments.getString(Config.NAME);
// Get the artist name
if (isArtist() || isAlbum()) {
mArtistName = mArguments.getString(Config.ARTIST_NAME);
}
displayView();
}
@Override
public void onBackPressed() {
// super.onBackPressed();
Intent start = new Intent(this,Base.class);
startActivity(start);
finish();
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putAll(mArguments);
}
private void displayView() {
// update the main content by replacing fragments
Fragment fragment = null ;
if(isAlbum()){
Toast.makeText(getBaseContext(), "isAlbum", Toast.LENGTH_LONG).show();
Bundle bundle=new Bundle();
bundle.putLong(Config.ID, ID);
fragment=new AlbumSong();
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.profileContainer, fragment).commit();
Toast.makeText(getBaseContext(), "Fragment Chnaged Succesfully", Toast.LENGTH_LONG).show();
// Action bar title = album name
setTitle(mProfileName);
}else{
if (isPlaylist()) {
// Add the carousel images
Toast.makeText(getBaseContext(), "isPlaylist", Toast.LENGTH_LONG).show();
Bundle bundle=new Bundle();
bundle.putLong(Config.ID, ID);
fragment=new PlaylistSong();
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.profileContainer, fragment).commit();
Toast.makeText(getBaseContext(), "Fragment Changed Successfully", Toast.LENGTH_LONG).show();
// Action bar title = album name
setTitle(mProfileName);
}
}}
private final boolean isPlaylist() {
return mType.equals(MediaStore.Audio.Playlists.CONTENT_TYPE);
}
private final boolean isArtist() {
return mType.equals(MediaStore.Audio.Artists.CONTENT_TYPE);
}
/**
* @return True if the MIME type is vnd.android.cursor.dir/albums, false
* otherwise.
*/
private final boolean isAlbum() {
return mType.equals(MediaStore.Audio.Albums.CONTENT_TYPE);
}
}
And If There is An Other way to do this i am All Ears. New Update in code please read comments , now when i set the PlaylistSong to display all the songs it works fine.Now For The Weird Part when i set the PlayPlaylist Method on the item click , it requires the PlaylistId to work guess what, it plays the songs in the playlist so i think that the problem is in displaying the song for this specific playlist.So the playlist id is null when displaying the playlist put is not when playing the playlist.Please Help And If Someone Needs More Code Tell Me.
PlayPlaylist():
public static void playPlaylist(final Context context, final long playlistId) {
final long[] playlistList = getSongListForPlaylist(context, playlistId);
if (playlistList != null) {
playAll(context, playlistList, -1, false);
}
}
getSongListForPlaylist():
public static final long[] getSongListForPlaylist(final Context context, final long playlistId) {
final String[] projection = new String[] {
Playlists.Members.AUDIO_ID
};
Cursor cursor = context.getContentResolver().query(
Playlists.Members.getContentUri("external",
Long.valueOf(playlistId)), projection, null, null,
Playlists.Members.DEFAULT_SORT_ORDER);
if (cursor != null) {
final long[] list = getSongListForCursor(cursor);
cursor.close();
cursor = null;
return list;
}
return sEmptyList;
}