1 |
package dk.thoerup.traininfo;
|
2 |
|
3 |
import java.util.HashMap;
|
4 |
import java.util.List;
|
5 |
import java.util.Map;
|
6 |
|
7 |
import android.content.Context;
|
8 |
import android.view.LayoutInflater;
|
9 |
import android.view.View;
|
10 |
import android.view.ViewGroup;
|
11 |
import android.widget.BaseAdapter;
|
12 |
import android.widget.ImageView;
|
13 |
import android.widget.TextView;
|
14 |
import dk.thoerup.android.traininfo.common.DepartureEntry;
|
15 |
|
16 |
public class DepartureListAdapter extends BaseAdapter {
|
17 |
|
18 |
boolean showDetails = false;
|
19 |
private List<DepartureEntry> departures;
|
20 |
LayoutInflater inflater;
|
21 |
Context context;
|
22 |
|
23 |
static Map<String,Integer> imageMap = new HashMap<String,Integer>();
|
24 |
static {
|
25 |
buildImageMap();
|
26 |
}
|
27 |
|
28 |
public DepartureListAdapter(Context context) {
|
29 |
super();
|
30 |
this.context = context;
|
31 |
|
32 |
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
33 |
}
|
34 |
|
35 |
public void setShowDetails(boolean showDetails) {
|
36 |
this.showDetails = showDetails;
|
37 |
}
|
38 |
|
39 |
public boolean getShowDetails() {
|
40 |
return showDetails;
|
41 |
}
|
42 |
|
43 |
public void toggleShowDetails() {
|
44 |
showDetails = !showDetails;
|
45 |
notifyDataSetChanged();
|
46 |
}
|
47 |
|
48 |
public void setDepartures(List<DepartureEntry> departures) {
|
49 |
this.departures = departures;
|
50 |
notifyDataSetChanged();
|
51 |
}
|
52 |
|
53 |
@Override
|
54 |
public int getCount() {
|
55 |
if (departures != null)
|
56 |
return departures.size();
|
57 |
else
|
58 |
return 0;
|
59 |
}
|
60 |
|
61 |
public DepartureEntry getDeparture(int position) {
|
62 |
return departures.get(position);
|
63 |
}
|
64 |
|
65 |
@Override
|
66 |
public Object getItem(int position) {
|
67 |
return null;
|
68 |
}
|
69 |
|
70 |
@Override
|
71 |
public long getItemId(int position) {
|
72 |
return position;
|
73 |
}
|
74 |
|
75 |
@Override
|
76 |
public View getView(int position, View convertView, ViewGroup parent) {
|
77 |
DepartureEntry station = departures.get(position);
|
78 |
|
79 |
View root = inflater.inflate(R.layout.departurerow , parent, false);
|
80 |
|
81 |
((TextView) root.findViewById(R.id.Time)).setText(station.getTime());
|
82 |
((TextView) root.findViewById(R.id.Destination)).setText(station.getDestination());
|
83 |
|
84 |
|
85 |
if ( (station.getStatus() != null && station.getStatus().length() > 0) || (station.getNote() != null && station.getNote().length() > 0) ) {
|
86 |
ImageView image = (ImageView) root.findViewById(R.id.InfoIcon);
|
87 |
|
88 |
|
89 |
String status = (station.getStatus() != null) ? station.getStatus().toLowerCase() : "";
|
90 |
String note = (station.getNote() != null) ? station.getNote().toLowerCase() : "";
|
91 |
|
92 |
int iconID;
|
93 |
if (status.indexOf("aflyst") > -1 || note.indexOf("aflyst") > -1 ) {
|
94 |
iconID = R.drawable.warn20;
|
95 |
} else if (note.indexOf("kører kun til") > -1 || note.indexOf("afgår fra") > -1) { //If these strings are present, the train only covers part of the line
|
96 |
iconID = R.drawable.warnyellow20;
|
97 |
} else {
|
98 |
iconID = R.drawable.info20;
|
99 |
}
|
100 |
|
101 |
image.setImageResource( iconID );
|
102 |
}
|
103 |
|
104 |
ImageView typeIcon = (ImageView) root.findViewById(R.id.TypeIcon);
|
105 |
String trainNumber = station.getTrainNumber().trim();
|
106 |
String trainParts[] = trainNumber.split(" ");
|
107 |
String code = trainParts[0].toLowerCase();
|
108 |
|
109 |
if (showDetails) {
|
110 |
root.findViewById(R.id.departureRow2).setVisibility( View.VISIBLE );
|
111 |
((TextView) root.findViewById(R.id.TrainNumber)).setText(trainNumber);
|
112 |
((TextView) root.findViewById(R.id.Origin)).setText( station.getOrigin() );
|
113 |
}
|
114 |
|
115 |
Integer imageId = imageMap.get(code);
|
116 |
if (imageId != null) {
|
117 |
typeIcon.setImageResource(imageId);
|
118 |
} else {
|
119 |
typeIcon.setImageResource(R.drawable.unknown);
|
120 |
}
|
121 |
|
122 |
return root;
|
123 |
}
|
124 |
|
125 |
//TODO: all these traintypes / icons should be explained somewhere
|
126 |
private static void buildImageMap() {
|
127 |
imageMap.put("ra", R.drawable.re); //RA = regionaltog arriva
|
128 |
imageMap.put("re", R.drawable.re); //RE = Regionaltog
|
129 |
imageMap.put("l", R.drawable.lyn); //L = Lyn
|
130 |
imageMap.put("ic", R.drawable.ic); //IC = Intercity
|
131 |
imageMap.put("pp", R.drawable.pp); //PP = Privatbaner (eg. odderbanen eller LokalBanen)
|
132 |
imageMap.put("ør", R.drawable.or); //ØR = Øresundstog
|
133 |
imageMap.put("ec", R.drawable.ec); //EC = EuroCity
|
134 |
imageMap.put("sj", R.drawable.sj); // SJ=Svenska Jernbaner
|
135 |
imageMap.put("ie", R.drawable.ie);
|
136 |
//TODO: missing IL: IntercityLyn Nonstop
|
137 |
|
138 |
imageMap.put("a", R.drawable.stog_a); //S-Tog: A banen #00b5f1
|
139 |
imageMap.put("b", R.drawable.stog_b); //S-Tog:B banen #5aba52
|
140 |
imageMap.put("bx", R.drawable.stog_bx); //S-Tog:Bx banen #a4d17d
|
141 |
imageMap.put("c", R.drawable.stog_c); //S-Tog:C banen #f89734
|
142 |
imageMap.put("e", R.drawable.stog_e); //S-Tog:E banen #837eba
|
143 |
imageMap.put("f", R.drawable.stog_f); //S-Tog:F banen #ffc32d
|
144 |
imageMap.put("h", R.drawable.stog_h); //S-Tog:H banen #f05737
|
145 |
|
146 |
|
147 |
|
148 |
}
|
149 |
|
150 |
}
|