Can you provide the data model you are using in your RecyclerView's adapter? I imagine that you may have something like this (this is just an example):
{
"id": 123123
"name": "Pizza",
"image": "http://yourimageurl...",
.
.
.
}
So what I would do is to display a new fragment or activity passing the object that the user clicked. In order to do so, this class must be Parcelable. This way you will have the data you need to display in the next screen, and if you need to make another request, let's say using the id of the Pizza, you can also do it.
To exemplify, let's say you have a "Meal" class, so you just have to make this class Parcelable:
@Parcelize
data class Meal(
val id: Long,
val name: String,
val imageUrl: String
) : Parcelable
After this, in your item click callback, just call the fragment/activity you use to display selection info:
fun onItemClickListener(meal: Meal) {
val intent = Intent(this, MealDetailActivity::class.java)
intent.putExtra("selected_meal", meal)
startActivity(intent)
}
And in your "MealDetailActivity" just recover your meal object and use it to populate your view or fetch the data you need.
Hope this helps, if not tell me and I will try to be more specific.
UPDATE: I add the Java solution.
The Meal class would be something like this:
public class Meal implements Parcelable {
private long id;
private String name;
private String image;
public Meal(long id, String name, String image) {
this.id = id;
this.name = name;
this.image = image;
}
protected Meal(Parcel in) {
id = in.readLong();
name = in.readString();
image = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeString(name);
dest.writeString(image);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Meal> CREATOR = new Creator<Meal>() {
@Override
public Meal createFromParcel(Parcel in) {
return new Meal(in);
}
@Override
public Meal[] newArray(int size) {
return new Meal[size];
}
};
}
As you can see, in Java the Parcelable is much more verbose (I strongly suggest you start coding in Kotlin :P). However, when you make a Java class implement the Parcelable interface, the AndroidStudio will ask you to add the Parcelable implementation for you (before this, you have to write the attributes you want this class to have). To clarify this, follow these steps:
- Create a new Java class
- Add the attributes that this class needs
- Write
implements Parcelable
- In the red lamp, click it and select "Add Parcelable implementation".
Then, in your item click callback, just call the fragment/activity you use to display selection info:
public void onMealItemClick(Meal meal) {
Intent intent = new Intent(this, MealDetailActivity.class);
intent.putExtra("selected_meal", meal);
startActivity(intent);
}
Finally, in your MealDetailActivity you can recover your Meal object:
@Override
public void onCreate(@Nullable Bundle onSavedInstanceState) {
Meal selectedMeal = getIntent().getParcelableExtra("selected_meal");
// do the stuff you need to do with your selected item.
}
As I said, I suggest you check how Parcelable works, since you will use it a lot in the future.
Hope this helps, if it does please mark as resolved :)