i have a recyclerview shows the list of movie, i want when the item movie clicked can pass data to detail using parcelable
this my viewHolderAdapter
public class MovieVHolder extends RecyclerView.ViewHolder {
TextView mTxtTitleMovie, mTxtDescriptionMovie, mTxtDateMovie;
ImageView mImgPosterMovie;
public MovieVHolder(@NonNull final View itemView) {
super(itemView);
mTxtTitleMovie = itemView.findViewById(R.id.txt_title_movie);
mTxtDescriptionMovie = itemView.findViewById(R.id.txt_desc_movie);
mTxtDateMovie = itemView.findViewById(R.id.txt_date_movie);
mImgPosterMovie = itemView.findViewById(R.id.img_movie);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, DetailActivity.class);
context.startActivity(i);
}
});
}
public void bind(ListMovieEntity listMovieEntity) {
mTxtTitleMovie.setText(listMovieEntity.getMovieTittle());
mTxtDescriptionMovie.setText(listMovieEntity.getMovieDescription());
mTxtDateMovie.setText(listMovieEntity.getMovieDate());
Glide.with(context)
.load("https://image.tmdb.org/t/p/w185/"+listMovieEntity.getMoviePosterPath())
.into(mImgPosterMovie);
}
}
and I've added parcelable in model class
change itemviewclick like this
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, DetailActivity.class);
//addthis i.putExtra(DetailActivity.MOVIE, entListMovie.get(getPosition()));
context.startActivity(i);
}
});
and in the detail make like this
add this
public static final String MOVIE = "movie";
in method onCreate() add this
YourList yourList = getIntent().getParcelableExtra(MOVIE);
after that, just set the data
textview.setText(yourList.getBlaBla());
Intent supports three ways to pass data:
Direct: put our data into intents directly
Bundle: create a bundle and set the data here
Parcelable: It is a way of “serializing” our object.
Passing data: Direct
Intent i = new Intent(context, DetailActivity.class);
i.putExtra("title", mTxtTitleMovie.getText().toString();
i.putExtra("surname", edtSurname.getText().toString();
i.putExtra("email", edtEmail.getText().toString();
context.startActivity(i);
Bundle
Intent i = new Intent(context, DetailActivity.class);
Bundle b = new Bundle();
b.putString("name", edtName.getText().toString());
b.putString("surname", edtSurname.getText().toString());
b.putString("email", edtEmail.getText().toString());
i.putExtra("personBdl", b);
context.startActivity(i);
Passing data: Parcelable
Let's suppose we have a class called Person that contains three attributes:name, surname and email.
Now if we want to pass this class it must implement the Parcelable interface like that
public class Person implements Parcelable {
private String name;
private String surname;
private String email;
// Get and Set methods
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(surname);
dest.writeString(email);
}
// We reconstruct the object reading from the Parcel data
public Person(Parcel p) {
name = p.readString();
surname = p.readString();
email = p.readString();
}
public Person() {}
// We need to add a Creator
public static final Parcelable.Creator<person> CREATOR = new Parcelable.Creator<person>() {
@Override
public Person createFromParcel(Parcel parcel) {
return new Person(parcel);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
Now we simply pass data like that:
Intent i = new Intent(EditActivity.this, ViewActivity.class);
Person p = new Person();
p.setName(edtName.getText().toString());
p.setSurname(edtSurname.getText().toString());
p.setEmail(edtEmail.getText().toString());
i.putExtra("myPers", p);
startActivity(i);
As you notice we simply put our object Person into the Intent. When we receive the data we have:
Bundle b = i.getExtras();
Person p = (Person) b.getParcelable("myPers");
String name = p.getName();
String surname = p.getSurname();
String email = p.getEmail();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.