1

我试图仅将数组中的播放器名称显示到列表视图中。这是我的代码:

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

}
4

2 回答 2

0

我不是 android listview 方面的专家,但如果它只是toString用来决定显示什么,则执行toString返回name

public String toString() { return name; }
于 2012-04-06T04:28:44.340 回答
0

使用CustomAdapter和设置getView(...) 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

于 2012-04-06T04:34:21.143 回答