--- android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java 2010/06/11 17:39:11 839 +++ android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java 2010/09/08 06:25:13 1028 @@ -19,6 +19,8 @@ import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; +import android.view.View; +import android.widget.ListView; import android.widget.TextView; import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.provider.TimetableProvider; @@ -27,10 +29,11 @@ public class TimetableList extends ListActivity { private static final int DLG_PROGRESS = 8000; - DepartureBean departure; + DepartureEntry departure; TimetableListAdapter adapter; TimetableFetcher fetcher; List timetables; + int commFailCounter = 0; TimetableProvider provider; @@ -48,7 +51,7 @@ Intent launchedBy = getIntent(); - departure = (DepartureBean) launchedBy.getSerializableExtra("departure"); + departure = (DepartureEntry) launchedBy.getSerializableExtra("departure"); ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber()); ((TextView)findViewById(R.id.Status)).setText(departure.getStatus()); @@ -56,6 +59,7 @@ ((TextView)findViewById(R.id.Note)).setText(departure.getNote()); ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this)); + ProviderFactory.purgeOldEntries(); //cleanup before fetching more data if (savedInstanceState == null) { startTimetableFetcher(); @@ -64,9 +68,36 @@ adapter.setTimetable(timetables); } } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (fetcher != null) { + fetcher.cancel(true); + } + } - - @Override + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + super.onListItemClick(l, v, position, id); + + TimetableBean tt = timetables.get(position); + + StationBean station = new StationBean(); + station.setName( tt.getStation() ); + station.setId( tt.getStationId() ); + station.setRegional(true); + + Intent intent = new Intent(this, DepartureList.class); + intent.putExtra("stationbean", station); + startActivity(intent); + + } + + + + @Override public void onSaveInstanceState(Bundle outState) { dismissDialog(DLG_PROGRESS); @@ -106,7 +137,6 @@ class TimetableFetcher extends AsyncTask { - boolean success; @Override protected void onPostExecute(Void result) { @@ -114,25 +144,31 @@ dismissDialog(DLG_PROGRESS); - if (success) { + if (timetables != null) { + commFailCounter = 0; + TimetableList.this.getListView().invalidateViews(); adapter.setTimetable(timetables); if (timetables.size() == 0) { - MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata)); + MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); } } else { // communication or parse error + commFailCounter++; AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); builder.setMessage(getString(timetablelist_fetcherror)); builder.setCancelable(true); - builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - startTimetableFetcher(); - - } - }); + if (commFailCounter < 3) { + builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startTimetableFetcher(); + + } + }); + } builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); + TimetableList.this.finish(); } }); @@ -150,8 +186,7 @@ protected Void doInBackground(String... arg0) { String type = arg0[0]; String trainID = arg0[1]; - success = provider.lookupTimetable(type, trainID); - timetables = provider.getTimetable(type, trainID); + timetables = provider.lookupTimetable(type, trainID); return null; }