|
1 package org.hedgewars.hedgeroid; |
|
2 |
|
3 import java.util.Comparator; |
|
4 |
|
5 import org.hedgewars.hedgeroid.R; |
|
6 import org.hedgewars.hedgeroid.Datastructures.Room; |
|
7 import org.hedgewars.hedgeroid.Datastructures.RoomWithId; |
|
8 import org.hedgewars.hedgeroid.util.ObservableTreeMapAdapter; |
|
9 |
|
10 import android.content.Context; |
|
11 import android.content.res.Resources; |
|
12 import android.view.LayoutInflater; |
|
13 import android.view.View; |
|
14 import android.view.ViewGroup; |
|
15 import android.widget.TextView; |
|
16 |
|
17 public class RoomlistAdapter extends ObservableTreeMapAdapter<String, RoomWithId> { |
|
18 private Context context; |
|
19 |
|
20 public RoomlistAdapter(Context context) { |
|
21 this.context = context; |
|
22 } |
|
23 |
|
24 @Override |
|
25 protected Comparator<RoomWithId> getEntryOrder() { |
|
26 return RoomWithId.NEWEST_FIRST_ORDER; |
|
27 } |
|
28 |
|
29 @Override |
|
30 public long getItemId(int position) { |
|
31 return getItem(position).id; |
|
32 } |
|
33 |
|
34 @Override |
|
35 public boolean hasStableIds() { |
|
36 return true; |
|
37 } |
|
38 |
|
39 private static CharSequence formatExtra(Resources res, Room room) { |
|
40 String ownermsg = res.getString(R.string.roomlist_owner, room.owner); |
|
41 String mapmsg = res.getString(R.string.roomlist_map, room.formatMapName(res)); |
|
42 String scheme = room.scheme.equals(room.weapons) ? room.scheme : room.scheme + " / " + room.weapons; |
|
43 String schememsg = res.getString(R.string.roomlist_scheme, scheme); |
|
44 return ownermsg + ". " + mapmsg + ", " + schememsg; |
|
45 } |
|
46 |
|
47 public View getView(int position, View convertView, ViewGroup parent) { |
|
48 View v = convertView; |
|
49 if (v == null) { |
|
50 LayoutInflater vi = LayoutInflater.from(context); |
|
51 v = vi.inflate(R.layout.listview_room, null); |
|
52 } |
|
53 |
|
54 Room room = getItem(position).room; |
|
55 int iconRes = room.inProgress ? R.drawable.roomlist_ingame : R.drawable.roomlist_preparing; |
|
56 |
|
57 if(v.findViewById(android.R.id.text1) == null) { |
|
58 // Tabular room list |
|
59 TextView roomnameView = (TextView)v.findViewById(R.id.roomname); |
|
60 TextView playerCountView = (TextView)v.findViewById(R.id.playercount); |
|
61 TextView teamCountView = (TextView)v.findViewById(R.id.teamcount); |
|
62 TextView ownerView = (TextView)v.findViewById(R.id.owner); |
|
63 TextView mapView = (TextView)v.findViewById(R.id.map); |
|
64 TextView schemeView = (TextView)v.findViewById(R.id.scheme); |
|
65 TextView weaponView = (TextView)v.findViewById(R.id.weapons); |
|
66 |
|
67 roomnameView.setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0); |
|
68 roomnameView.setText(room.name); |
|
69 if(playerCountView != null) { |
|
70 playerCountView.setText(String.valueOf(room.playerCount)); |
|
71 } |
|
72 if(teamCountView != null) { |
|
73 teamCountView.setText(String.valueOf(room.teamCount)); |
|
74 } |
|
75 ownerView.setText(room.owner); |
|
76 mapView.setText(room.formatMapName(context.getResources())); |
|
77 schemeView.setText(room.scheme); |
|
78 weaponView.setText(room.weapons); |
|
79 } else { |
|
80 // Small room list |
|
81 TextView v1 = (TextView)v.findViewById(android.R.id.text1); |
|
82 TextView v2 = (TextView)v.findViewById(android.R.id.text2); |
|
83 |
|
84 v1.setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0); |
|
85 v1.setText(room.name); |
|
86 v2.setText(formatExtra(context.getResources(), room)); |
|
87 } |
|
88 |
|
89 return v; |
|
90 } |
|
91 } |