--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/02/01 16:49:02 572 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/07/10 16:03:10 981 @@ -1,12 +1,13 @@ package dk.thoerup.traininfo; -import static dk.thoerup.traininfo.R.string.departurelist_fetchdata; +import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures; +import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals; import static dk.thoerup.traininfo.R.string.generic_cancel; import static dk.thoerup.traininfo.R.string.generic_retry; + import java.text.NumberFormat; -import java.util.ArrayList; -import java.util.List; + import android.app.AlertDialog; import android.app.Dialog; @@ -19,6 +20,8 @@ import android.os.Bundle; import android.util.Log; import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import dk.thoerup.traininfo.provider.DepartureProvider; @@ -32,7 +35,7 @@ DepartureListAdapter adapter; DepartureProvider provider; - List departures; + DepartureBean departures; int selectedItemId; //DepartureBean currentDeparture; @@ -43,7 +46,9 @@ StationBean station; - @SuppressWarnings("unchecked") + boolean arrival = false; + + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -61,22 +66,50 @@ ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); + final Button departureBtn = (Button) findViewById(R.id.departurebtn); + final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); - findViewById(R.id.header).setOnClickListener( mapLauncher ); + departureBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View arg0) { + arrivalBtn.setBackgroundResource(R.drawable.custom_button); + departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); + arrival = false; + startDepartureFetcher(); + } + }); + arrivalBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View arg0) { + arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); + departureBtn.setBackgroundResource(R.drawable.custom_button); + arrival = true; + startDepartureFetcher(); + } + }); - NumberFormat format = NumberFormat.getNumberInstance(); - format.setMaximumFractionDigits(1); - format.setMinimumFractionDigits(1); - + + + findViewById(R.id.header).setOnClickListener( mapLauncher ); + int distance = station.getDistance(); - ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); - station.setRegional(true); - Log.e("ASD", "" + station.isRegional() + "/" + station.isSTrain() + "/" + station.isMetro()); + if (distance != 0) { + NumberFormat format = NumberFormat.getNumberInstance(); + format.setMaximumFractionDigits(1); + format.setMinimumFractionDigits(1); + + ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); + } else { + ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); + } + if (station.isRegional() == false && station.isSTrain() == false) { getListView().setVisibility( View.GONE ); findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); + departureBtn.setVisibility( View.GONE ); + arrivalBtn.setVisibility(View.GONE); } else { provider = ProviderFactory.getDepartureProvider(); @@ -84,8 +117,8 @@ if (savedInstanceState == null) { startDepartureFetcher(); } else { - departures = (List) savedInstanceState.getSerializable("departures"); - adapter.setDepartures(departures); + departures = (DepartureBean) savedInstanceState.getSerializable("departures"); + adapter.setDepartures(departures.entries); selectedItemId = savedInstanceState.getInt("selectedItemId"); } } @@ -99,16 +132,27 @@ outState.putInt("selectedItemId", selectedItemId); - outState.putSerializable("departures", (ArrayList) departures); + outState.putSerializable("departures", departures); } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (fetcher != null) { + fetcher.cancel(true); + } + } + + @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); selectedItemId = position; - DepartureBean dep = departures.get(selectedItemId); + DepartureEntry dep = departures.entries.get(selectedItemId); Intent intent = new Intent(this, TimetableList.class); intent.putExtra("departure", dep); @@ -125,6 +169,8 @@ switch (id) { case DLG_PROGRESS: pgDialog = (ProgressDialog) dialog; + int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; + pgDialog.setMessage( getString(messageId) ); break; } } @@ -133,8 +179,8 @@ protected Dialog onCreateDialog(int id) { switch (id) { case DLG_PROGRESS: + ProgressDialog dlg = new ProgressDialog(this); - dlg.setMessage( getString(departurelist_fetchdata) ); dlg.setCancelable(true); return dlg; default: @@ -184,9 +230,12 @@ pgDialog.dismiss(); if (success) { - adapter.setDepartures(departures); - if (departures.size() == 0) { - MessageBox.showMessage(DepartureList.this, "No departures found"); + DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html + adapter.setDepartures(departures.entries); + DepartureList.this.getListView().setVisibility(View.VISIBLE); + + if (departures.entries.size() == 0) { + MessageBox.showMessage(DepartureList.this, "No departures found", true); } } else { // communication or parse error AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); @@ -202,16 +251,22 @@ builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); + DepartureList.this.finish(); } - }); - builder.show(); + }); + + try { + builder.show(); + } catch (android.view.WindowManager.BadTokenException e) { + Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running + } } } @Override protected Void doInBackground(Integer... params) { - success = provider.lookupDepartures(params[0]); - departures = provider.getDepartures(params[0]); + success = provider.lookupDepartures(params[0], DepartureList.this.arrival); + departures = provider.getDepartures(params[0], DepartureList.this.arrival); return null; }