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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1710 - (hide annotations) (download)
Tue Mar 6 12:29:26 2012 UTC (12 years, 2 months ago) by torben
File size: 20967 byte(s)
also set an count on add/remove favorites
1 torben 237 package dk.thoerup.traininfo;
2    
3 torben 1053 import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals;
4 torben 835 import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures;
5     import static dk.thoerup.traininfo.R.string.generic_cancel;
6     import static dk.thoerup.traininfo.R.string.generic_retry;
7    
8 torben 237 import java.text.NumberFormat;
9    
10 torben 336 import android.app.AlertDialog;
11 torben 237 import android.app.Dialog;
12     import android.app.ListActivity;
13     import android.app.ProgressDialog;
14 torben 1433 import android.content.ActivityNotFoundException;
15 torben 336 import android.content.DialogInterface;
16 torben 237 import android.content.Intent;
17 torben 1446 import android.content.SharedPreferences;
18 torben 1053 import android.graphics.Typeface;
19 torben 239 import android.net.Uri;
20 torben 237 import android.os.AsyncTask;
21     import android.os.Bundle;
22 torben 1446 import android.preference.PreferenceManager;
23 torben 630 import android.util.Log;
24 torben 982 import android.view.Menu;
25     import android.view.MenuItem;
26 torben 237 import android.view.View;
27 torben 835 import android.view.View.OnClickListener;
28     import android.widget.Button;
29 torben 237 import android.widget.ListView;
30 torben 1053 import android.widget.TableLayout;
31     import android.widget.TableRow;
32 torben 237 import android.widget.TextView;
33 torben 1433 import android.widget.Toast;
34 torben 1066 import dk.thoerup.android.traininfo.common.DepartureBean;
35     import dk.thoerup.android.traininfo.common.DepartureEntry;
36     import dk.thoerup.android.traininfo.common.MetroBean;
37     import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry;
38 torben 1408 import dk.thoerup.android.traininfo.common.StationEntry;
39 torben 255 import dk.thoerup.traininfo.provider.DepartureProvider;
40 torben 1053 import dk.thoerup.traininfo.provider.MetroProvider;
41 torben 253 import dk.thoerup.traininfo.provider.ProviderFactory;
42 torben 1709
43     import dk.thoerup.traininfo.util.FavoritesHelper;
44 torben 245 import dk.thoerup.traininfo.util.MessageBox;
45 torben 1562 import dk.thoerup.traininfo.util.StationEntryCsv;
46 torben 237
47     public class DepartureList extends ListActivity {
48    
49     public static final int DLG_PROGRESS = 1;
50 torben 982 static final int MENU_MAP = 100;
51     static final int MENU_NOTIFICATIONS = 101;
52 torben 1434 static final int MENU_METROMAP = 102;
53     static final int MENU_TOGGLEDETAILS= 103;
54 torben 1709
55     static final int MENU_FAVORITES_ADD = 104;
56     static final int MENU_FAVORITES_REMOVE = 105;
57 torben 237
58 torben 362
59 torben 237 DepartureListAdapter adapter;
60     DepartureProvider provider;
61 torben 981 DepartureBean departures;
62 torben 237
63 torben 1053 MetroBean metroBean;
64     MetroProvider metro;
65    
66 torben 257 int selectedItemId;
67 torben 1709
68     FavoritesHelper favorites;
69    
70 torben 257 //DepartureBean currentDeparture;
71 torben 237
72     ProgressDialog pgDialog;
73 torben 362
74 torben 238 DepartureFetcher fetcher;
75 torben 1053 MetroFetcher metroFetcher;
76 torben 557
77 torben 1066 StationEntry station;
78 torben 238
79 torben 1250 String trainType = "REGIONAL";
80    
81 torben 1434 boolean arrival = false;
82 torben 981
83 torben 1017 int commFailCounter = 0;
84    
85 torben 237 @Override
86     protected void onCreate(Bundle savedInstanceState) {
87     super.onCreate(savedInstanceState);
88     setContentView(R.layout.departurelist);
89 torben 1709
90     favorites = new FavoritesHelper(this);
91    
92 torben 237 adapter = new DepartureListAdapter(this);
93 torben 1446 setListAdapter(adapter);
94    
95 torben 237 Intent launchedBy = getIntent();
96 torben 557
97 torben 1066 station = (StationEntry) launchedBy.getSerializableExtra("stationbean");
98 torben 239
99 torben 557 ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
100 torben 317
101 torben 557
102     ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
103 torben 237
104 torben 835 final Button departureBtn = (Button) findViewById(R.id.departurebtn);
105     final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
106 torben 1044 final Button metroBtn = (Button) findViewById(R.id.metrobtn);
107 torben 1250 final Button regionalBtn = (Button) findViewById(R.id.regionalbtn);
108     final Button stogBtn = (Button) findViewById(R.id.stogbtn);
109 torben 294
110 torben 1053 final View metroView = findViewById(R.id.metroonly);
111    
112 torben 835 departureBtn.setOnClickListener( new OnClickListener() {
113     @Override
114     public void onClick(View arg0) {
115     arrivalBtn.setBackgroundResource(R.drawable.custom_button);
116     departureBtn.setBackgroundResource(R.drawable.custom_button_hilight);
117 torben 1044 metroBtn.setBackgroundResource(R.drawable.custom_button);
118 torben 1053
119     getListView().setVisibility( View.VISIBLE );
120     metroView.setVisibility( View.GONE );
121 torben 835 arrival = false;
122     startDepartureFetcher();
123     }
124     });
125     arrivalBtn.setOnClickListener( new OnClickListener() {
126     @Override
127     public void onClick(View arg0) {
128     arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
129     departureBtn.setBackgroundResource(R.drawable.custom_button);
130 torben 1044 metroBtn.setBackgroundResource(R.drawable.custom_button);
131 torben 1053
132     getListView().setVisibility( View.VISIBLE );
133     metroView.setVisibility( View.GONE );
134 torben 835 arrival = true;
135     startDepartureFetcher();
136     }
137     });
138    
139 torben 1250 regionalBtn.setOnClickListener( new OnClickListener() {
140     @Override
141     public void onClick(View arg0) {
142     regionalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
143     stogBtn.setBackgroundResource(R.drawable.custom_button);
144     metroBtn.setBackgroundResource(R.drawable.custom_button);
145    
146     departureBtn.setVisibility( View.VISIBLE );
147     arrivalBtn.setVisibility( View.VISIBLE );
148    
149     getListView().setVisibility( View.VISIBLE );
150     metroView.setVisibility( View.GONE );
151     trainType = "REGIONAL";
152     startDepartureFetcher();
153     }
154     });
155     stogBtn.setOnClickListener( new OnClickListener() {
156     @Override
157     public void onClick(View arg0) {
158     regionalBtn.setBackgroundResource(R.drawable.custom_button);
159     stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);
160     metroBtn.setBackgroundResource(R.drawable.custom_button);
161    
162    
163     departureBtn.setVisibility( View.VISIBLE );
164     arrivalBtn.setVisibility( View.VISIBLE );
165    
166     getListView().setVisibility( View.VISIBLE );
167     metroView.setVisibility( View.GONE );
168     trainType = "STOG";
169     startDepartureFetcher();
170     }
171     });
172    
173    
174    
175 torben 1044 metroBtn.setOnClickListener( new OnClickListener() {
176     @Override
177     public void onClick(View v) {
178 torben 1250 regionalBtn.setBackgroundResource(R.drawable.custom_button);
179     stogBtn.setBackgroundResource(R.drawable.custom_button);
180 torben 1044 metroBtn.setBackgroundResource(R.drawable.custom_button_hilight);
181 torben 1053
182 torben 1250 departureBtn.setVisibility( View.GONE );
183     arrivalBtn.setVisibility( View.GONE );
184    
185 torben 1053 getListView().setVisibility( View.GONE );
186     metroView.setVisibility( View.VISIBLE );
187     startMetroFetcher();
188 torben 1044 }
189     });
190 torben 835
191    
192    
193 torben 1044
194 torben 982 // findViewById(R.id.header).setOnClickListener( mapLauncher );
195 torben 237
196 torben 1066 int distance = station.getCalcdist();
197 torben 742 if (distance != 0) {
198     NumberFormat format = NumberFormat.getNumberInstance();
199     format.setMaximumFractionDigits(1);
200     format.setMinimumFractionDigits(1);
201 torben 575
202 torben 742 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
203     } else {
204     ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
205     }
206 torben 552
207 torben 1028 ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
208 torben 1044
209 torben 1562 Log.e("Station", StationEntryCsv.toCSV(station) );
210 torben 1044
211 torben 1446
212    
213 torben 1044 if (station.isMetro() == false) {
214     metroBtn.setVisibility( View.GONE );
215     }
216 torben 1028
217 torben 1053 metro = ProviderFactory.getMetroProvider();
218    
219 torben 1250 if (station.isRegional() == false ) {
220     regionalBtn.setVisibility(View.GONE);
221     }
222    
223     if (station.isStrain() == false ) {
224     stogBtn.setVisibility(View.GONE);
225     }
226    
227     if (station.isRegional() == true && station.isStrain() == false ) {
228     if ( station.isMetro() == false )
229     regionalBtn.setVisibility(View.GONE);
230     trainType = "REGIONAL";
231     }
232    
233     if (station.isRegional() == false && station.isStrain() == true) {
234     if (station.isMetro() == false)
235     stogBtn.setVisibility(View.GONE);
236    
237     stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);
238     trainType = "STOG";
239    
240     }
241 torben 1446 //Both enabled - use preferred from preferences
242     if (station.isRegional() == true && station.isStrain() == true ) {
243     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
244     trainType = prefs.getString("traintype", "REGIONAL"); //default value is gps
245    
246     if (trainType.equals("STOG") ) {
247     stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);
248     regionalBtn.setBackgroundResource(R.drawable.custom_button);
249     }
250     }
251 torben 1250
252    
253 torben 1066 if (station.isRegional() == false && station.isStrain() == false) {
254 torben 552 getListView().setVisibility( View.GONE );
255 torben 1053 metroView.setVisibility( View.VISIBLE );
256    
257 torben 835 departureBtn.setVisibility( View.GONE );
258     arrivalBtn.setVisibility(View.GONE);
259 torben 1250 metroBtn.setVisibility( View.GONE );
260 torben 552
261 torben 1250
262    
263 torben 1053 if (savedInstanceState == null) {
264     startMetroFetcher();
265     } else {
266     metroBean = (MetroBean) savedInstanceState.getSerializable("metro");
267     loadMetroData();
268     }
269    
270 torben 257 } else {
271 torben 552 provider = ProviderFactory.getDepartureProvider();
272    
273     if (savedInstanceState == null) {
274     startDepartureFetcher();
275     } else {
276 torben 981 departures = (DepartureBean) savedInstanceState.getSerializable("departures");
277 torben 990
278     if ( (departures != null) && (departures.entries != null) ) {
279     adapter.setDepartures(departures.entries);
280     }
281 torben 982 selectedItemId = savedInstanceState.getInt("selectedItemId");
282    
283     if ( hasNotifications() ) {
284     findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
285     }
286    
287 torben 552 }
288 torben 257 }
289 torben 237 }
290    
291 torben 1446
292    
293 torben 982 boolean hasNotifications() {
294     return (departures != null && departures.notifications.size() > 0);
295     }
296    
297 torben 243 @Override
298     public void onSaveInstanceState(Bundle outState)
299     {
300 torben 257 if (pgDialog != null && pgDialog.isShowing())
301     dismissDialog(DLG_PROGRESS);
302 torben 362
303 torben 257 outState.putInt("selectedItemId", selectedItemId);
304    
305 torben 981 outState.putSerializable("departures", departures);
306 torben 1053 outState.putSerializable("metro", metroBean);
307 torben 243 }
308 torben 918
309    
310 torben 243
311 torben 237 @Override
312 torben 918 protected void onDestroy() {
313     super.onDestroy();
314    
315     if (fetcher != null) {
316     fetcher.cancel(true);
317     }
318 torben 1053
319     if (metroFetcher != null) {
320     metroFetcher.cancel(true);
321     }
322 torben 918 }
323    
324     @Override
325 torben 237 protected void onListItemClick(ListView l, View v, int position, long id) {
326     super.onListItemClick(l, v, position, id);
327 torben 362
328 torben 1487 //how can this happen ??
329     if (departures == null || departures.entries == null || departures.entries.size() == 0) {
330     Toast.makeText(this, "No departures in list ?!?", Toast.LENGTH_LONG).show(); //TODO: translate
331     return;
332     }
333    
334 torben 362 selectedItemId = position;
335 torben 364
336 torben 981 DepartureEntry dep = departures.entries.get(selectedItemId);
337 torben 237
338 torben 362 Intent intent = new Intent(this, TimetableList.class);
339 torben 364 intent.putExtra("departure", dep);
340 torben 362
341     startActivity(intent);
342    
343 torben 237 }
344    
345    
346     @Override
347     protected void onPrepareDialog(int id, Dialog dialog) {
348     super.onPrepareDialog(id, dialog);
349 torben 257
350 torben 237 switch (id) {
351     case DLG_PROGRESS:
352     pgDialog = (ProgressDialog) dialog;
353 torben 835 int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
354     pgDialog.setMessage( getString(messageId) );
355 torben 237 break;
356     }
357     }
358    
359     @Override
360     protected Dialog onCreateDialog(int id) {
361     switch (id) {
362     case DLG_PROGRESS:
363 torben 835
364 torben 237 ProgressDialog dlg = new ProgressDialog(this);
365     dlg.setCancelable(true);
366 torben 362 return dlg;
367 torben 237 default:
368     return super.onCreateDialog(id);
369     }
370     }
371 torben 982
372    
373 torben 336
374 torben 982 @Override
375 torben 1442 public boolean onPrepareOptionsMenu(Menu menu) {
376     super.onPrepareOptionsMenu(menu);
377 torben 1709
378     ///////////////////////
379 torben 1442
380     MenuItem item = menu.findItem( MENU_NOTIFICATIONS );
381     boolean notifEnabled = hasNotifications();
382     item.setEnabled(notifEnabled);
383 torben 1709
384     //////////////////////////
385    
386    
387 torben 1442
388 torben 1709
389 torben 1442 return true;
390     }
391    
392     @Override
393 torben 982 public boolean onCreateOptionsMenu(Menu menu) {
394     MenuItem item;
395 torben 985
396 torben 1434
397     item = menu.add(0, MENU_TOGGLEDETAILS, 0, getString(R.string.departurelist_toggledetails));
398     item.setIcon(android.R.drawable.ic_menu_view);
399    
400 torben 985 item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
401 torben 982 item.setIcon(android.R.drawable.ic_menu_mapmode);
402    
403 torben 985 item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
404 torben 1057 item.setIcon(android.R.drawable.ic_menu_info_details);
405 torben 982
406     boolean notifEnabled = hasNotifications();
407     item.setEnabled(notifEnabled);
408 torben 1709
409     ///////////////////////////////////////
410     if ( ! favorites.hasFavorite( this.station.getId() ) ) {
411     item = menu.add(0, MENU_FAVORITES_ADD, 0, getString(dk.thoerup.traininfo.R.string.stationlist_addfavorite) );
412     } else {
413     item = menu.add(0, MENU_FAVORITES_REMOVE, 0, getString(dk.thoerup.traininfo.R.string.stationlist_removefavorite) );
414     }
415 torben 1710 item.setIcon(android.R.drawable.ic_menu_save);
416 torben 1709 /////////////////////////////
417 torben 982
418 torben 1057 if (station.isMetro()) {
419     item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?!
420     item.setIcon(android.R.drawable.ic_menu_mapmode);
421 torben 1434 }
422 torben 982
423     return true;
424     }
425    
426     @Override
427     public boolean onOptionsItemSelected(MenuItem item) {
428 torben 1434 boolean res = true;
429 torben 982 switch(item.getItemId()) {
430     case MENU_MAP:
431 torben 1434 Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16");
432    
433     try {
434 torben 1432 startActivity( new Intent(Intent.ACTION_VIEW, uri));
435     } catch (ActivityNotFoundException anfe) {
436     Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show();
437     }
438 torben 1434
439 torben 982 break;
440     case MENU_NOTIFICATIONS:
441     Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
442     i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
443 torben 1434 startActivity(i);
444 torben 982 break;
445 torben 1057 case MENU_METROMAP:
446     Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class);
447 torben 1434 startActivity(metroMap);
448 torben 1057 break;
449 torben 1434 case MENU_TOGGLEDETAILS:
450     adapter.toggleShowDetails();
451     break;
452 torben 1709 case MENU_FAVORITES_ADD:
453     favorites.addFavorite( this.station.getId() );
454     Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationadded), Toast.LENGTH_SHORT).show();
455     break;
456     case MENU_FAVORITES_REMOVE:
457     favorites.removeFavorite( station.getId() );
458     Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationremoved), Toast.LENGTH_SHORT).show();
459     break;
460 torben 982 default:
461     res = super.onOptionsItemSelected(item);
462     }
463     return res;
464     }
465    
466 torben 336 void startDepartureFetcher() {
467     showDialog(DLG_PROGRESS);
468     fetcher = new DepartureFetcher();
469 torben 557 fetcher.execute(station.getId());
470 torben 336 }
471 torben 316
472 torben 1053 void startMetroFetcher() {
473     showDialog(DLG_PROGRESS);
474     metroFetcher = new MetroFetcher();
475     metroFetcher.execute(station.getId());
476     }
477    
478 torben 237 class DialogDismisser implements View.OnClickListener {
479    
480     Dialog dlg;
481     public DialogDismisser(Dialog d) {
482     dlg = d;
483     }
484    
485     @Override
486     public void onClick(View v) {
487     if (dlg.isShowing())
488     dlg.dismiss();
489 torben 365 }
490     }
491 torben 238
492 torben 982 /*View.OnClickListener mapLauncher = new View.OnClickListener() {
493 torben 239 @Override
494 torben 557 public void onClick(View v) {
495     Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
496 torben 239 startActivity( new Intent(Intent.ACTION_VIEW, uri));
497     }
498 torben 982 };*/
499 torben 336
500    
501 torben 238
502 torben 310 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
503 torben 238
504     @Override
505     protected void onPostExecute(Void result) {
506     super.onPostExecute(result);
507    
508 torben 336
509 torben 238 pgDialog.dismiss();
510 torben 1423
511 torben 1373 if (departures != null && departures.errorCode == null) {
512 torben 1017 commFailCounter = 0;
513 torben 917 DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
514 torben 981 adapter.setDepartures(departures.entries);
515 torben 917 DepartureList.this.getListView().setVisibility(View.VISIBLE);
516    
517 torben 982
518 torben 1441 // handle notification icon.
519     View notifIcon = findViewById(R.id.notifIcon);
520     if ( hasNotifications() ) {
521 torben 1261 notifIcon.setVisibility(View.VISIBLE);
522     notifIcon.setClickable(true);
523     notifIcon.setOnClickListener( new View.OnClickListener() {
524     @Override
525     public void onClick(View v) {
526     Intent i = new Intent(DepartureList.this, dk.thoerup.traininfo.NotificationList.class);
527     i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
528     startActivity(i);
529     }
530     });
531 torben 1441 } else {
532     notifIcon.setVisibility(View.INVISIBLE);
533     }
534 torben 982
535 torben 1264 if (departures.entries.size() == 0) {
536 torben 1262 int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals;
537     MessageBox.showMessage(DepartureList.this, getString(msgId), false);
538 torben 319 }
539     } else { // communication or parse error
540 torben 1017 commFailCounter++;
541 torben 1373 AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
542    
543     if (departures != null && departures.errorCode != null ) { //got an error xml back
544     commFailCounter = 10;
545     builder.setMessage( getString(R.string.no_backend) );
546     } else {
547     builder.setMessage( getString(R.string.departurelist_fetcherror) );
548     }
549 torben 336 builder.setCancelable(true);
550 torben 1373
551 torben 1017 if (commFailCounter < 3) {
552     builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
553     public void onClick(DialogInterface dialog, int id) {
554     dialog.dismiss();
555     startDepartureFetcher();
556    
557     }
558     });
559     }
560 torben 561 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
561 torben 336 public void onClick(DialogInterface dialog, int id) {
562     dialog.dismiss();
563 torben 843 DepartureList.this.finish();
564 torben 336 }
565 torben 630 });
566    
567 torben 1263 try { //TODO: is this still necessary after the 0.9.4.1 fix ?
568 torben 630 builder.show();
569     } catch (android.view.WindowManager.BadTokenException e) {
570     Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
571     }
572 torben 319 }
573 torben 238 }
574    
575     @Override
576 torben 310 protected Void doInBackground(Integer... params) {
577 torben 1250 departures = provider.lookupDepartures(params[0], DepartureList.this.arrival, trainType);
578 torben 238 return null;
579     }
580    
581     }
582 torben 1053
583     public void loadMetroData() {
584     ((TextView) findViewById(R.id.operations)).setText( metroBean.operationInfo );
585     ((TextView) findViewById(R.id.plan)).setText( metroBean.plan );
586    
587    
588     TableLayout table = (TableLayout) findViewById(R.id.metrotable);
589     table.removeAllViews();
590    
591     TableRow head = new TableRow(this);
592    
593     TextView h1 = new TextView(this);
594     h1.setText("Metro");
595 torben 1072 h1.setTextSize(16);
596 torben 1053 h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
597    
598    
599     TableRow.LayoutParams params = new TableRow.LayoutParams();
600     params.span = 2;
601     head.addView(h1, params);
602    
603 torben 1055
604    
605 torben 1053 TextView h2 = new TextView(this);
606 torben 1072 h2.setTextSize(16);
607 torben 1053 h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
608 torben 1055 h2.setText("Om minutter");
609    
610 torben 1072 params = new TableRow.LayoutParams();
611     params.weight = 2;
612 torben 1055 head.addView(h2,params);
613 torben 1053
614    
615    
616     table.addView(head);
617    
618     for (MetroEntry entry : metroBean.entries) {
619     TableRow row = new TableRow(this);
620    
621     Log.e("Test", "" + entry.destination);
622    
623     TextView v1 = new TextView(this);
624 torben 1072 v1.setTextSize(16);
625     v1.setText( entry.metro );
626 torben 1053 row.addView(v1);
627    
628     TextView v2 = new TextView(this);
629 torben 1072 v2.setTextSize(16);
630 torben 1053 v2.setText( entry.destination );
631     row.addView(v2);
632 torben 1055
633 torben 1053 TextView v3 = new TextView(this);
634 torben 1072 v3.setTextSize(16);
635 torben 1053 v3.setText( entry.minutes );
636     row.addView(v3);
637    
638     table.addView(row);
639    
640     }
641     findViewById(R.id.rootView).requestLayout();
642     }
643    
644     class MetroFetcher extends AsyncTask<Integer, Void, Void> {
645    
646     @Override
647     protected void onPostExecute(Void result) {
648     super.onPostExecute(result);
649    
650    
651    
652     pgDialog.dismiss();
653    
654     if (metroBean != null) {
655     loadMetroData();
656     } else { // communication or parse error
657     commFailCounter++;
658     AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
659     builder.setMessage("Error finding metro data");
660     builder.setCancelable(true);
661     if (commFailCounter < 3) {
662     builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
663     public void onClick(DialogInterface dialog, int id) {
664     dialog.dismiss();
665     startMetroFetcher();
666    
667     }
668     });
669     }
670     builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
671     public void onClick(DialogInterface dialog, int id) {
672     dialog.dismiss();
673 torben 1263 DepartureList.this.finish(); //TODO: should we really close the activity ??
674 torben 1053 }
675     });
676    
677 torben 1263 try { //TODO: is this still necessary after the 0.9.4.1 fix ?
678 torben 1053 builder.show();
679     } catch (android.view.WindowManager.BadTokenException e) {
680     Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
681     }
682     }
683     }
684    
685     @Override
686     protected Void doInBackground(Integer... params) {
687     metroBean = metro.lookupMetroInfo(params[0]);
688     return null;
689     }
690    
691     }
692    
693 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20