2014-02-12 62 views
-1

我正在創建一個應用程序,我必須一遍又一遍地引用相同的數據。我是新來的編碼,所以我不知道我是否正確地做了。現在我用setter和getters來創建一個類來存儲我想要的數據。保存ArrayList項目

(Item類)

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); 

    } 
} 

編輯數據並將其添加到一個單獨的數組列表後,如何可以參考回數據。例如在應用程序的其他部分,我需要參考玩家的分數?我想將數據保存在SharedPreferences中,但它只保存集合。請幫忙。我想把它發送到另一個活動。例如,我希望它在主屏幕上顯示玩家分數。以及在一個談論球隊球員的屏幕上。

+0

你的意思是與應用程序的另一部分?例如,您的問題是否將數據發送給其他活動? – armandooj

回答

0

如果您不想將數據直接傳遞給使用它的函數,則必須將其保存在某處。這被稱爲數據持久性。它通常意味着該過程將在創建數據之前死亡。在Java中,這個術語是鬆散地用來指代事件之間持續的數據,甚至是方法調用,而不一定是過程。在Java中,持久性通常使用序列化完成。你可以讓你Serializable類這樣

​​

一個對象只能進行序列化,如果每一個對象包含可序列化。一旦一個對象是可序列化的,它就可以寫入一個流中(也可以寫入一個文件)並從中讀取。其他代碼可以檢索序列化的數據。 ObjectOutputStreamObjectInputStream能夠讀取和寫入可串行化的數據。在讀取對象到相應的類型時,您必須將這些對象進行投射。

如果我讀了你的問題錯了,你真的只是不知道如何訪問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()); 
} 
+0

你有語法錯誤(缺少';')。 'score'也是私人的,所以你需要使用getter方法來訪問它。 – Sionnach733

+1

@ Sionnach733在您引起我的注意之後,我重新構建了第一部分代碼,因爲我之前錯過了OP想要完成的任務。我95%肯定他不關心持久性,只希望Stack做他的功課。 – Rainbolt