无法在新活动中检索额外数据
Posted
技术标签:
【中文标题】无法在新活动中检索额外数据【英文标题】:Can't retrieve extra data in new activity 【发布时间】:2020-12-09 20:33:38 【问题描述】:当我点击 Recycler 视图中的 CardView 时,我已将 Parcelable 实现为我想在活动之间传递的对象。唯一的问题是对象(ResultsHelperClass)在接收到意图时总是有修改的值。
我现在完全陷入困境,因为我似乎找不到问题所在。
我作为额外对象传递给意图的对象具有正确的值,但无论我如何尝试接收它,它的值总是会发生变化。
我的 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:ResultsHelperClasslocalScore=0, visitorScore=0, teamId=1, teamName='Sénior Masculí', localLogoUrl='https://pitudev.com/sacabaneta/rivals/scabaneta.jpg', visitorLogoUrl='https://www.pitudev.com/scabaneta/rivals/sonservera.jpg', local='1', date='2020-08-16 18:30:00.000000'
在新活动(团队)中,我这样做是为了从所选项目中检索数据:
if (getIntent().hasExtra("game"))
ResultsHelperClass selected_game = getIntent().getParcelableExtra("game");
System.out.println(selected_game);
这会打印这些值:
I/System.out:ResultsHelperClasslocalScore=15,visitorScore=15269971,teamId=6881390,teamName='null',localLogoUrl='null',visitorLogoUrl='null',local='null',date='空'
这对我来说毫无意义,因为我已经检查过我是否传递了正确的值...如果有人可以提供帮助,我将不胜感激...谢谢
【问题讨论】:
【参考方案1】:固定...
问题是 writeToParcel() 和 ResultsHelperClass(Parcel in) 属性需要处于相同的顺序...
希望这对未来的人有所帮助!
【讨论】:
以上是关于无法在新活动中检索额外数据的主要内容,如果未能解决你的问题,请参考以下文章
单击 Listview 中的项目时,如何在新活动中从 Firebase 检索数据?
通过 REST API 检索结果并将其显示在新活动的列表视图中(列表视图出现小问题)