gpt4 book ai didi

java - Android 的数组和 ListView (java)

转载 作者:行者123 更新时间:2023-12-01 05:23:03 25 4
gpt4 key购买 nike

我试图将数组中的玩家姓名显示到 ListView 中。这是我的代码:

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

}

最佳答案

使用CustomAdapter并在getView(...)方法中设置

喜欢,

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

关于java - Android 的数组和 ListView (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10039096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com