Search code examples
androidjsonandroid-recyclerviewrx-javaaudio-playerandroid-music-player

How to receive JSON data from recyclerView to another activity


I'm making a Music Player app. Songs are fetched from JSON url and I have a recyclerView to display the data. Now clicking on the recyclerView another music player activity launches and starts playing the song. Here is the problem, clicking on the recyclerview only pass the clicked index data to another activity. Now I want to play next song from the music player activity. How can I do that ? I'm using retrofit and rxjava to fetch the data.

Here is the recyclerView :

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Context context = view.getContext();

            Intent intent = new Intent(context , MusicPlayerActivity.class);
            intent.putExtra(Intent.EXTRA_TEXT , mAndroidList.get(position).getDesc());
            intent.putExtra("url", mAndroidList.get(position).getAudio());

            context.startActivity(intent);
            ((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    });

App.java

public class App implements Serializable {

@SerializedName("itemId")
@Expose
private String itemId;
@SerializedName("desc")
@Expose
private String desc;
@SerializedName("audio")
@Expose
private String audio;

public App(String itemId, String desc, String audio) {
    this.itemId = itemId;
    this.desc = desc;
    this.audio = audio;

}


public String getId() {
    return itemId;
}

public String getDesc() {
    return desc;
}

public String getAudio() {
    return audio;
}

}


Solution

  • Try to pass using Bundle,

    Context context = view.getContext();
    
    Intent intent = new Intent(context , MusicPlayerActivity.class);
    Bundle bundle = new Bundle();
    bundle.putSerializable(Intent.EXTRA_TEXT, mAndroidList);
    bundle.putInt("POSITION", position);
    intent.putExtras(bundle);
    
    context.startActivity(intent);
    ((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    

    In MusicPlayerActivity.class use below code to get data,

    Intent intent = this.getIntent();
    Bundle bundle = intent.getExtras();
    int position = bundle.getInt("POSITION");
    List<App> list = (List<App>)bundle.getSerializable(Intent.EXTRA_TEXT);
    

    Thanks.