2012-04-06 28 views
1

我想顯示數組中的播放器名稱到列表視圖中。這是我的代碼:android(java)的數組和列表視圖

player_List.setAdapter(new ArrayAdapter<Player>(this, android.R.layout.simple_list_item_1, dataStore.getPlayers())); 

即時得到使用此調用數組中的所有信息。我不確定如何叫這個名字。

這是我的數據存儲的編碼:

/** 
* This class is essentially a global library for the Scoresheet. 
* It provides methods through which the Players and Teams can be accessed 
* from any part of the application. 
* The saving/loading of application data will also be handled through this 
* class. 
* 
* You can access this DataStore by calling: 
* DataStore dataStore = ((DataStore)getApplicationContext()); 
* From any Activity 
*/ 
public class DataStore extends Application { 

    // Create ArrayLists to hold all our Player and Team objects 
    private ArrayList<Player> players = new ArrayList<Player>(); 
    private ArrayList<Team> teams = new ArrayList<Team>(); 

    // File names for our internal storage: 
    private String playerFileName = "players"; 
    private String teamFileName = "teams"; 

    /** 
    * Add a Player object to the list of players. 
    * @param p The Player object to add 
    */ 
    public void addPlayer(Player p){ 
     this.players.add(p); 

    } 

    /** 
    * Merge an ArrayList of Player objects with the current collection of Players 
    * @param players ArrayList of Player objects to add to the collection 
    */ 
    public void addPlayers(ArrayList<Player> players){ 
     Iterator<Player> it = players.iterator(); 
     while (it.hasNext()){ 
      this.players.add(it.next()); 
     } 
    } 

    /** 
    * Return an ArrayList of player objects containing all Players 
    * @return 
    */ 
    public ArrayList<Player> getPlayers(){ 
     return this.players; 
    } 

,這是我的播放器類:

public class Player implements Serializable{ 
    // Randomly generate serial ID 
    private static final long serialVersionUID = 7423594865734681292L; 
    private static int ID = 0; // Class variable 
    public String name; 
    private int id; 

    public Player(String name) throws Exception{ 
     this.setId(ID); 
     ID++;    // Increment class ID counter 
     if (!this.setName(name)) 
      throw new Exception("Invalid Name"); // This is the only way to prevent the object being instantiated if it has an invalid name 
    } 

    public String getName() { 
     return name; 
    } 

    /** 
    * Set the players name as desired. 
    * @param name 
    * @return true on success, false on fail 
    */ 
    public boolean setName(String name) { 
     // Only update the name if we are actually given a string 
     boolean success = false; 
     name = name.trim(); 
     if (!name.equals("")){ 
      this.name = name; 
      success = true; 
     } 
     return success; 
    } 

    public int getId() { 
     return id; 
    } 

    private void setId(int id) { 
     this.id = id; 
    } 

} 

回答

0

使用CustomAdaptergetView(...) method

等設定,

Player player = getPlayers().get(position); 
textview.setText(player.name) 

看到這個預報
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx

+0

所以基本上你可以創建一個播放器即。在文件中輸入玩家名稱。我想在列表視圖中顯示玩家的名字。所以每次打開應用程序時,以前保存的所有玩家名稱都可以在列表視圖中看到。 – 2012-04-06 06:29:19

+0

但不知道如何做到這一點作爲代碼我有像「[email protected]」 – 2012-04-06 06:33:31

+0

「我不知道這是什麼(」[email protected]「)的地方信息?'但做出一些努力我給你的解決方案的路徑,並試圖瞭解上面的兩個鏈接的例子 – 2012-04-06 06:56:18

0

我在Android的ListView方面的專家,但如果它只是使用toString到決定展示什麼,實施toString以返回name

public String toString() { return name; }