1 package org.hedgewars.hedgeroid.netplay; |
|
2 |
|
3 import java.util.ArrayList; |
|
4 import java.util.Collections; |
|
5 import java.util.Comparator; |
|
6 import java.util.List; |
|
7 |
|
8 import org.hedgewars.hedgeroid.R; |
|
9 |
|
10 import android.content.Context; |
|
11 import android.database.DataSetObserver; |
|
12 import android.util.Pair; |
|
13 import android.view.LayoutInflater; |
|
14 import android.view.View; |
|
15 import android.view.ViewGroup; |
|
16 import android.widget.BaseAdapter; |
|
17 import android.widget.TextView; |
|
18 |
|
19 public class PlayerlistAdapter extends BaseAdapter { |
|
20 private List<Pair<Player, Long>> players = new ArrayList<Pair<Player, Long>>(); |
|
21 private Context context; |
|
22 private Playerlist playerlist; |
|
23 |
|
24 private DataSetObserver observer = new DataSetObserver() { |
|
25 @Override |
|
26 public void onChanged() { |
|
27 reloadFromList(playerlist); |
|
28 } |
|
29 |
|
30 @Override |
|
31 public void onInvalidated() { |
|
32 invalidate(); |
|
33 } |
|
34 }; |
|
35 |
|
36 public PlayerlistAdapter(Context context) { |
|
37 this.context = context; |
|
38 } |
|
39 |
|
40 public int getCount() { |
|
41 return players.size(); |
|
42 } |
|
43 |
|
44 public Player getItem(int position) { |
|
45 return players.get(position).first; |
|
46 } |
|
47 |
|
48 public long getItemId(int position) { |
|
49 return players.get(position).second; |
|
50 } |
|
51 |
|
52 public boolean hasStableIds() { |
|
53 return true; |
|
54 } |
|
55 |
|
56 public void setList(Playerlist playerlist) { |
|
57 if(this.playerlist != null) { |
|
58 this.playerlist.unregisterObserver(observer); |
|
59 } |
|
60 this.playerlist = playerlist; |
|
61 this.playerlist.registerObserver(observer); |
|
62 reloadFromList(playerlist); |
|
63 } |
|
64 |
|
65 public void invalidate() { |
|
66 if(playerlist != null) { |
|
67 playerlist.unregisterObserver(observer); |
|
68 } |
|
69 playerlist = null; |
|
70 notifyDataSetInvalidated(); |
|
71 } |
|
72 |
|
73 private void reloadFromList(Playerlist list) { |
|
74 players = new ArrayList<Pair<Player, Long>>(list.getMap().values()); |
|
75 Collections.sort(players, AlphabeticalOrderComparator.INSTANCE); |
|
76 notifyDataSetChanged(); |
|
77 } |
|
78 |
|
79 public View getView(int position, View convertView, ViewGroup parent) { |
|
80 View v = convertView; |
|
81 if (v == null) { |
|
82 LayoutInflater vi = LayoutInflater.from(context); |
|
83 v = vi.inflate(R.layout.listview_player, null); |
|
84 } |
|
85 |
|
86 String player = players.get(position).first.name; |
|
87 TextView username = (TextView) v.findViewById(android.R.id.text1); |
|
88 username.setText(player); |
|
89 return v; |
|
90 } |
|
91 |
|
92 private static final class AlphabeticalOrderComparator implements Comparator<Pair<Player, Long>> { |
|
93 public static final AlphabeticalOrderComparator INSTANCE = new AlphabeticalOrderComparator(); |
|
94 public int compare(Pair<Player, Long> lhs, Pair<Player, Long> rhs) { |
|
95 return lhs.first.name.compareToIgnoreCase(rhs.first.name); |
|
96 }; |
|
97 } |
|
98 } |
|