Arguments persist

Hello, I have had this problem for several months, so please help me, I am starting to develop android apps, so I could not fix it.

Problem: While developing a music player my problem is with playlists and its id, when I pass the PlaylistId under arguments that return nothing, so the screen stays white. Now I have tried many things to fix this and I managed to figure out that the problem is with the arguments ("Adapter is ok, layout is ok, everything else is ok") So please, I know it will take a while and works Please put 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 another way to do it, I am all ears. New update in code, please read the comments, now that I have installed PlaylistSong to display all the songs it works fine. Now for The Weird Part, when I set the PlayPlaylist method on the click element, it requires the PlaylistId to work on what it is playing. songs in the playlist, so I think the problem is displaying the song for that particular playlist. So the playlist ID is null when the playlist is not displayed when the playlist is played. Please help and if anyone needs more code please 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;
}

      

+3


source to share





All Articles