/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/DepartureListAdapter.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/DepartureListAdapter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 490 - (hide annotations) (download)
Thu Oct 29 19:27:29 2009 UTC (14 years, 7 months ago) by torben
File size: 3689 byte(s)
Reuse the existing views if possible
1 torben 237 package dk.thoerup.traininfo;
2    
3 torben 486 import java.util.HashMap;
4 torben 237 import java.util.List;
5 torben 486 import java.util.Map;
6 torben 237
7     import android.content.Context;
8 torben 490 import android.util.Log;
9 torben 237 import android.view.LayoutInflater;
10     import android.view.View;
11     import android.view.ViewGroup;
12     import android.widget.BaseAdapter;
13 torben 244 import android.widget.ImageView;
14 torben 237 import android.widget.TextView;
15    
16     public class DepartureListAdapter extends BaseAdapter {
17    
18     private List<DepartureBean> departures;
19     LayoutInflater inflater;
20     Context context;
21 torben 486
22     static Map<String,Integer> imageMap = new HashMap<String,Integer>();
23     static {
24     buildImageMap();
25     }
26    
27 torben 237 public DepartureListAdapter(Context context) {
28     super();
29     this.context = context;
30    
31     inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
32 torben 486
33 torben 237 }
34    
35     public void setDepartures(List<DepartureBean> departures) {
36     this.departures = departures;
37     notifyDataSetChanged();
38     }
39    
40     @Override
41     public int getCount() {
42     if (departures != null)
43     return departures.size();
44     else
45     return 0;
46     }
47    
48     public DepartureBean getDeparture(int position) {
49     return departures.get(position);
50     }
51    
52     @Override
53     public Object getItem(int position) {
54     return null;
55     }
56    
57     @Override
58     public long getItemId(int position) {
59     return position;
60     }
61    
62     @Override
63     public View getView(int position, View convertView, ViewGroup parent) {
64     DepartureBean station = departures.get(position);
65    
66    
67 torben 490 View root;
68     if (convertView == null || convertView.getId() != R.id.DepartureRow ) {
69     root = inflater.inflate(R.layout.departurerow , parent, false);
70     } else {
71     root = convertView;
72     }
73    
74    
75 torben 237 ((TextView) root.findViewById(R.id.Time)).setText(station.getTime());
76     ((TextView) root.findViewById(R.id.Destination)).setText(station.getDestination());
77    
78 torben 244 if (station.getStatus().length() > 0 || station.getNote().length() > 0) {
79 torben 250 ImageView image = (ImageView) root.findViewById(R.id.InfoIcon);
80 torben 483
81     int iconID;
82     if (station.getStatus().toLowerCase().indexOf("aflyst") > -1 ||
83     station.getNote().toLowerCase().indexOf("aflyst") > -1 ) {
84     iconID = R.drawable.warn20;
85     } else {
86     iconID = R.drawable.info20;
87     }
88    
89     image.setImageResource( iconID );
90 torben 244 }
91 torben 237
92 torben 250 ImageView typeIcon = (ImageView) root.findViewById(R.id.TypeIcon);
93 torben 351 String trainNumber = station.getTrainNumber().trim();
94 torben 486 String code = trainNumber.split(" ")[0].toLowerCase();
95    
96     Integer imageId = imageMap.get(code);
97     if (imageId != null) {
98     typeIcon.setImageResource(imageId);
99     } else {
100 torben 333 typeIcon.setImageResource(R.drawable.unknown);
101 torben 486 }
102 torben 250
103 torben 237 return root;
104     }
105 torben 486
106     private static void buildImageMap() {
107     imageMap.put("ra", R.drawable.re); //RA = regionaltog arriva
108     imageMap.put("re", R.drawable.re); //RE = Regionaltog
109     imageMap.put("l", R.drawable.lyn); //L = Lyn
110     imageMap.put("ic", R.drawable.ic); //IC = Intercity
111     imageMap.put("pp", R.drawable.pp); //PP = Privatbaner (eg. odderbanen eller LokalBanen)
112     imageMap.put("ør", R.drawable.or); //ØR = Øresundstog
113     imageMap.put("ec", R.drawable.ec); //EC = EuroCity
114     imageMap.put("sj", R.drawable.sj); // SJ=Svenska Jernbaner
115    
116     imageMap.put("a", R.drawable.stog_a); //S-Tog: A banen #00b5f1
117     imageMap.put("b", R.drawable.stog_b); //S-Tog:B banen #5aba52
118     imageMap.put("bx", R.drawable.stog_bx); //S-Tog:Bx banen #a4d17d
119     imageMap.put("c", R.drawable.stog_c); //S-Tog:C banen #f89734
120     imageMap.put("e", R.drawable.stog_e); //S-Tog:E banen #837eba
121     imageMap.put("f", R.drawable.stog_f); //S-Tog:F banen #ffc32d
122     imageMap.put("h", R.drawable.stog_h); //S-Tog:H banen #f05737
123    
124 torben 237
125 torben 486
126     }
127    
128 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20