【发布时间】: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 的问题?
标签: java android arrays list save