我的应用程序中有以下课程:
public class Player implements Parcelable {
private String name;
private int playerColor;
private int[] scores;
...
}
如何告诉ArrayAdapter的getView方法在关联的listview中显示分数数组的哪个元素?我想基于活动中的信息,在ListView中显示player.name字段以及player.scores数组的特定元素。
public class PlayerGameAdapter extends ArrayAdapter<Player> {
private ArrayList<Player> entries;
private Activity activity;
public PlayerGameAdapter(Activity a, int viewResourceId, ArrayList<Player> entries) {
super(a, viewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder {
public Button buttonName;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listview_row_player_game, null);
holder = new ViewHolder();
holder.buttonName = (Button) v.findViewById(R.id.buttonGamePlayerListName);
v.setTag(holder);
} else
holder = (ViewHolder) v.getTag();
final MiniGolfPlayer player = entries.get(position);
if (player != null) {
holder.buttonName.setText(player.getName() + " - " + player.scores[ELEMENT]);
}
return v;
}
}
最佳答案
将位置变量添加到播放器类。
public class Player implements Parcelable {
private String name;
private int playerColor;
private int[] scores;
private int position; // also add the get and set methods
}
在活动中,您可以更改要分别显示给每个玩家的分数的位置。
((Player)entries.get(0)).setPosition(AnyValueYouWant);
((Player)entries.get(1)).setPosition(AnotherValue);
然后通知您的适配器刷新listview:
PlayerGameAdapter.notifyDataSetChanged();
那对你有用吗。或者您期望其他的东西。
关于android - 如何将信息传递到自定义ArrayAdapter的getView中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19037243/