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