I have implemented Parcelable into an Object I want to pass between activities when I click on a CardView inside a Recycler View. The only problem is the object (ResultsHelperClass) always has modified values upon receiving the intent.
I'm completely stuck at the moment as I can't seem to find the problem.
The object I pass into the intent as an extra has the correct values, but no matter how I try to receive it it always has the values changed.
My ResultsHelperClass
public class ResultsHelperClass implements Parcelable {
int localScore, visitorScore, teamId;
String teamName, localLogoUrl, visitorLogoUrl, local, date;
//Constructor
public ResultsHelperClass(String localLogoUrl, String visitorLogoUrl, String local, String date, int teamId, int localScore, int visitorScore, String teamName) {
this.localScore = localScore;
this.visitorScore = visitorScore;
this.teamName = teamName;
this.teamId = teamId;
this.localLogoUrl = localLogoUrl;
this.visitorLogoUrl = visitorLogoUrl;
this.local = local;
this.date = date;
}
//Constructor
public ResultsHelperClass() {
this.localScore = 0;
this.visitorScore = 0;
this.teamId = 0;
this.teamName = "";
this.localLogoUrl = "";
this.visitorLogoUrl = "";
this.local = "";
this.date = "";
}
protected ResultsHelperClass(Parcel in) {
localScore = in.readInt();
visitorScore = in.readInt();
teamId = in.readInt();
teamName = in.readString();
localLogoUrl = in.readString();
visitorLogoUrl = in.readString();
local = in.readString();
date = in.readString();
}
public static final Creator<ResultsHelperClass> CREATOR = new Creator<ResultsHelperClass>() {
@Override
public ResultsHelperClass createFromParcel(Parcel in) {
return new ResultsHelperClass(in);
}
@Override
public ResultsHelperClass[] newArray(int size) {
return new ResultsHelperClass[size];
}
};
//Getters
public int getLocalScore() {
return localScore;
}
public int getVisitorScore() {
return visitorScore;
}
public String getTeamName() {
return teamName;
}
public int getTeamId() {
return teamId;
}
public String getLocal() {
return local;
}
public String getDate() {
return date;
}
public String getLocalLogoUrl() {
return localLogoUrl;
}
public String getVisitorLogoUrl() {
return visitorLogoUrl;
}
//Setters
public void setLocalScore(int localScore) {
this.localScore = localScore;
}
public void setVisitorScore(int visitorScore) {
this.visitorScore = visitorScore;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public void setTeamId(int teamId) {
this.teamId = teamId;
}
public void setLocalLogoUrl(String localLogoUrl) {
this.localLogoUrl = localLogoUrl;
}
public void setVisitorLogoUrl(String visitorLogoUrl) {
this.visitorLogoUrl = visitorLogoUrl;
}
public void setLocal(String local) {
this.local = local;
}
public void setDate(String date) {
this.date = date;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(teamName);
dest.writeString(localLogoUrl);
dest.writeString(visitorLogoUrl);
dest.writeString(local);
dest.writeString(date);
dest.writeInt(localScore);
dest.writeInt(visitorScore);
dest.writeInt(teamId);
}
@Override
public String toString() {
return "ResultsHelperClass{" +
"localScore=" + localScore +
", visitorScore=" + visitorScore +
", teamId=" + teamId +
", teamName='" + teamName + '\'' +
", localLogoUrl='" + localLogoUrl + '\'' +
", visitorLogoUrl='" + visitorLogoUrl + '\'' +
", local='" + local + '\'' +
", date='" + date + '\'' +
'}';
}
}
I print the item selected before sending it to the Extra intent:
@Override
public void onGameClick(int position) {
Intent in = new Intent(this, Team.class);
System.out.println(listResults.get(position));
in.putExtra("game", listResults.get(position));
startActivity(in);
}
This prints the correct values for the selected item:
I/System.out: ResultsHelperClass{localScore=0, visitorScore=0, teamId=1, teamName='Sénior Masculí ', localLogoUrl='https://pitudev.com/sacabaneta/rivals/sacabaneta.jpg', visitorLogoUrl='https://www.pitudev.com/sacabaneta/rivals/sonservera.jpg', local='1', date='2020-08-16 18:30:00.000000'
In the new activity (Team), i do this to retrieve the data from the selected item:
if (getIntent().hasExtra("game")){
ResultsHelperClass selected_game = getIntent().getParcelableExtra("game");
System.out.println(selected_game);
}
This prints these values:
I/System.out: ResultsHelperClass{localScore=15, visitorScore=15269971, teamId=6881390, teamName='null', localLogoUrl='null', visitorLogoUrl='null', local='null', date='null'}
This makes no sense to me since I have checked that i pass the correct values... I would be so grateful if someone could help... Thanks