-1

I am creating a music player. I want to play a selected song from ListView. But when i click on the specific item(song) in listview. I am not getting the item(song) i clicked in second class. The player always selects first song from list and plays the first song. I think that there is problem in code. Please check and correct. Thanks

Tab1(Sending Class)

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    int songIndex = position;

    // Starting new intent
    Intent in = new Intent(getActivity(), NowPlaying.class);
    // Sending songIndex to PlayerActivity
    in.putExtra("songIndex", songIndex);
    getActivity().setResult(100, in);
    // Closing PlayListView
    getActivity().finish();
    startActivity(in);
}

NowPlaying(Receiving Class)

 /**
 * Receiving song index from playlist view
 * and play the song
 */
@Override
protected void onActivityResult(int requestCode,
                                int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == 100) {
        currentSongIndex = data.getExtras().getInt("songIndex");
        // play selected song
        playSong(currentSongIndex);
    }

}
Akshay Sharma
  • 65
  • 1
  • 9
  • Are those 2 classes of different apps? Why don't you pass an extra with the intent? Anyways you are not startingForResult. It's a bit confusing – user6547359 Jul 08 '16 at 09:41
  • How does your onCreate method in NowPlaying.class look like? – Murat Karagöz Jul 08 '16 at 09:41
  • @user6547359 These two classes are of same app. In first class, songs are loaded in listView. When i click on any song, the class which receives songIndex is not playing that song. – Akshay Sharma Jul 08 '16 at 09:46
  • @AkshaySharma probably because your result is never 100 there. Pass it as extra as you do with the index, and start it in the oncreate (or on resume). When you start activity for result you expect that the started activity returns a result, not viceversa. – user6547359 Jul 08 '16 at 09:48

3 Answers3

0

This might help you https://developer.android.com/training/basics/intents/result.html

From the first activity you need to call startActivityForResult() instead of startActivity(). Don't call finish() on this activity after that

And from the activity that is going to return the result you just set the result before finishing using finish() the activity don't call startActivity() to invoke the old activity.

Do not use class name for the intent constructor in second activity. User default intent constructor

Bhushan
  • 205
  • 2
  • 14
0

To pass the position of the song

Tab1

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Intent in = new Intent(getActivity(), NowPlaying.class);
    in.putExtra("songIndex", position);
    startActivity(in);
    getActivity().finish();

}

NowPlaying(Receiving Class)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name); //insert here you activity's xml layout
    currentSongIndex = getExtras().getInt("songIndex"); 
 // currentSongIndex now holds an integer value, the position of the song selected from your list. After that you should call your function to play the specific song
}
Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37
0

See there are two things :

  1. If you want to pass it through Intent object and want to receive it in another class you need to receive it in onCreate like this :

    In NowPlaying(Receiving Class)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        currentSongIndex = getIntent().getStringExtra("songIndex");               
    }
    
  2. If you want to do it by in onActivityResult then your NowPlaying activity must have started Tab1Activity with startActivityForResult(). And no need on starting an activity in onListItemClick in Tab1


EDIT :

You need to under stand the difference i guess : Difference between startActivityForResult() and startActivity()?

Community
  • 1
  • 1
Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59