Reputation: 57
I have the following class in my application:
public class Player implements Parcelable {
private String name;
private int playerColor;
private int[] scores;
...
}
How do I tell the getView method of my ArrayAdapter which element of the scores array to display in the associated listview? I want to display in the ListView the player.name field, as well as a particular element of the player.scores array, based on information from the activity.
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;
}
}
Upvotes: 1
Views: 319
Reputation: 24195
add position variable to your Player Class.
public class Player implements Parcelable {
private String name;
private int playerColor;
private int[] scores;
private int position; // also add the get and set methods
}
On your activity you can change the position of the score you want to show for every player individually.
((Player)entries.get(0)).setPosition(AnyValueYouWant);
((Player)entries.get(1)).setPosition(AnotherValue);
Then notify your adapter to refresh listview:
PlayerGameAdapter.notifyDataSetChanged();
Does that work for you. or you expect something else.
Upvotes: 1