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

私はアンドロイドリストビューの専門家ではありませんが、表示するものを決定するために使用するだけの場合は、単に返すようtoStringに実装します:toStringname

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 に答える