我的申请表中有以下课程:
public class Player implements Parcelable {
private String name;
private int playerColor;
private int[] scores;
...
}
如何告诉我的ArrayAdapter的getView方法在相关的列表视图中显示得分数组的哪个元素?我想根据活动中的信息在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;
}
}
答案 0 :(得分:1)
将位置变量添加到您的玩家类。
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();
这对你有用吗?或者你期待别的东西。