我已经将parcelable实现为一个对象,当我在recycler视图中单击cardview时,我想在活动之间传递它。唯一的问题是对象(resultshelperclass)在接收到意图时总是有修改过的值。
我现在完全卡住了,好像找不到问题。
我作为额外对象传递到intent的对象具有正确的值,但是无论我如何尝试接收它,它总是具有更改的值。
我的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 + '\'' +
'}';
}
}
我在将所选项目发送到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);
}
这将打印所选项目的正确值:
i/system.out:resultshelperclass{localscore=0,visitorscore=0,teamid=1,teamname='sé尼奥马斯库尔í ', locallogourl='https://pitudev.com/sacabaneta/rivals/sacabaneta.jpg',visitorlogourl='https://www.pitudev.com/sacabaneta/rivals/sonservera.jpg'本地'1',日期'2020-08-16 18:30:00.000000'
在新活动(团队)中,我这样做是为了从所选项目中检索数据:
if(getintent().hasextra(“游戏”)){
ResultsHelperClass selected_game = getIntent().getParcelableExtra("game");
System.out.println(selected_game);
}
这将打印这些值:
i/system.out:resultshelperclass{localscore=15,visitorscore=15269971,teamid=6881390,teamname='null',locallogourl='null',visitorlogourl='null',local='null',date='null'}
这对我来说毫无意义,因为我已经检查了我是否传递了正确的值。。。如果有人能帮上忙我会非常感激的。。。谢谢
1条答案
按热度按时间oknwwptz1#
固定的。。。
问题是writetoparcel()和resultshelperclass(parcel-in)属性的顺序必须相同。。。
希望这对将来的人有帮助!