-1

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;
}

}

Community
  • 1
  • 1
Saket Kumar
  • 463
  • 8
  • 22

3 Answers3

1

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.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
0

In my experience, the best way I could come up with RecyclerView is to extract out the click listeners to the views (activities/fragment or ...).

I Think in your scenario it's a good practice to implement the listener in the activity too.

So keep a reference to your music objects in your activity and then when a click happens just pass the list to another activity along with the index of the selected music.

here is the overview of how it will probably look like:

public class ListActivity extends AppCompatActivity{

ArrayList<App> musicList;
RecyclerView recyclerView;
Adapter adapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    musicList = getMusics();
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter = new Adapter(musicList);
    adapter.setListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int index =(int) v.getTag();

            Intent intent = new Intent(ListActivity.this , MusicPlayerActivity.class);
            intent.putExtra("index" , index);
            intent.putExtra("music_list", musicList);

            startActivity(intent);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    });
    recyclerView.setAdapter(adapter);

}
}

and the adapter:

public static class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private View.OnClickListener listener;

    public Adapter(List<App> musics) {
        this.musics = musics;
    }

    private List<App> musics;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //create your view holder here
        return null;
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        //bind the view
//            .
//            .
            //remember to store the index to be able to retrieve it later
            holder.itemView.setTag(position);
            holder.itemView.setOnClickListener(listener);
        }
    @Override
    public int getItemCount() {
        return musics.size();
    }

    public void setListener(View.OnClickListener listener) {
        this.listener = listener;
    }

hope it helps

seyed Jafari
  • 1,235
  • 10
  • 20
0

Use Gson to convert json data to string

Example.

intent.putExtra("data", new Gson().toJson(your json object).

And in activity receive it like

Data = new Gson().fromJson(getIntent.getStringExtra("data"), YourModel.class);

SANDIP CHAUDHARI
  • 166
  • 1
  • 2
  • 5