保存 ArrayList 项 [关闭]
Posted
技术标签:
【中文标题】保存 ArrayList 项 [关闭]【英文标题】:Saving ArrayList Item [closed] 【发布时间】:2014-02-12 16:27:13 【问题描述】:我正在创建一个应用程序,我必须在其中一遍又一遍地引用相同的数据。我是编码新手,所以我不知道我是否做得正确。现在我用 setter 和 getter 创建了一个类来存储我想要的数据。
(物品类)
public class PlayerList
private String score;
private String team;
private String name;
private String scoreWeek;
private String position;
private String goals;
private String assists;
private String yellowCards;
private String redCards;
private int iconID;
public PlayerList(String name, String team, String score, String scoreWeek, String position,
String goals, String assists, String yellowCards, String redCards, int iconID)
super();
this.score= score;
this.team = team;
this.name = name;
this.scoreWeek = scoreWeek;
this.position= position;
this.goals = goals;
this.assists = assists;
this.yellowCards = yellowCards;
this.redCards = redCards;
this.iconID= iconID;
public String getScore()
return score;
public void setScore(String score)
this.score = score;
public String getTeam()
return team;
public void setTeam(String team)
this.team = team;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getScoreWeek()
return scoreWeek;
public void setScoreWeek(String scoreWeek)
this.scoreWeek = scoreWeek;
public String getPosition()
return position;
public void setPosition(String position)
this.position = position;
public String getGoals()
return goals;
public void setGoals(String goals)
this.goals = goals;
public String getAssists()
return assists;
public void setAssists(String assists)
this.assists = assists;
public String getYellowCards()
return yellowCards;
public void setYellowCards(String yellowCards)
this.yellowCards = yellowCards;
public String getRedCards()
return redCards;
public void setRedCards(String redCards)
this.redCards = redCards;
public int getIconID()
return iconID;
public void setIconID(int iconID)
this.iconID = iconID;
然后我创建了另一个类,在其中编辑这些数据并将其放入一个数组中。
(编辑代码)
public class Players extends Activity
private ArrayList<PlayerList> stateList = new ArrayList<PlayerList>();
public Players()
PlayerList _states = new PlayerList("Name","Team","Score","Score Week", "Position",
"Goals", "Assists","Yellow Cards","Red Cards", R.drawable.client_icon);
stateList.add(_states);
PlayerList _states1 = new PlayerList("Name2","Team","Score","Score Week", "Position",
"Goals", "Assists","Yellow Cards","Red Cards", R.drawable.client_icon);
stateList.add(_states1);
PlayerList _states2 = new PlayerList("Name3","Team","Score","Score Week", "Position",
"Goals", "Assists","Yellow Cards","Red Cards", R.drawable.client_icon);
stateList.add(_states2);
在编辑数据并将其添加到单独的数组 List 之后,我如何才能引用回数据。例如,在应用程序的其他部分中,我需要参考玩家的分数?我想将数据保存在 SharedPreferences 中,但它只保存集合。请帮忙。我想将它发送到另一个活动。例如,我希望它在主屏幕上显示玩家得分。以及在谈论球队球员的屏幕上。
【问题讨论】:
应用程序的另一部分是什么意思?例如,您是否有将数据发送到另一个 Activity 的问题? 【参考方案1】:如果您不想将数据直接传递给使用它的函数,则必须将其保存在某个地方。这称为数据持久性。这通常意味着该进程将在它创建的数据之前终止。在 Java 中,该术语用于泛指在事件甚至方法调用之间持续存在的数据,不一定是进程。在 Java 中,持久性通常使用序列化来完成。你可以让你的班级Serializable
像这样
public class MyClass implements Serializable
只有当它包含的每个对象都是可序列化的时,才能使对象可序列化。一旦一个对象是可序列化的,它就可以被写入和读取一个流(可能是一个文件)。然后其他代码可以检索序列化数据。 ObjectOutputStream
和 ObjectInputStream
能够读写可序列化的数据。在将对象读回适当的类型时,您必须将它们强制转换。
如果我读错了你的问题,而你真的不知道如何访问你的ArrayList
,试试这个:
int score = null;
for (PlayerList pl: myArrayList)
if(pl.getName() == teamName)
score = pl.getScore();
System.out.println(score == null ? "No score available.": score);
或者,要获得所有团队的分数:
for (PlayerList pl: myArrayList)
System.out.println(pl.getScore());
【讨论】:
您有语法错误(缺少;
)。 score
也是私有的,所以你需要使用 getter 方法来访问它。
@Sionnach733 在您引起我的注意后,我重新构建了第一段代码,因为我之前错过了 OP 试图完成的任务。我 95% 确定他根本不在乎坚持,只想让 Stack 做他的功课。以上是关于保存 ArrayList 项 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章