1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
import java.text.NumberFormat; |
import java.text.NumberFormat; |
4 |
|
import java.util.ArrayList; |
5 |
import java.util.List; |
import java.util.List; |
6 |
|
|
7 |
import android.app.Dialog; |
import android.app.Dialog; |
14 |
import android.view.View; |
import android.view.View; |
15 |
import android.widget.ListView; |
import android.widget.ListView; |
16 |
import android.widget.TextView; |
import android.widget.TextView; |
|
import android.widget.Toast; |
|
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
|
17 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
18 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
19 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
20 |
|
|
21 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
27 |
DepartureProvider provider; |
DepartureProvider provider; |
28 |
List<DepartureBean> departures; |
List<DepartureBean> departures; |
29 |
|
|
30 |
static int itemId; |
int selectedItemId; |
31 |
static DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
32 |
|
|
33 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
34 |
|
Dialog detailsDialog; |
35 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
36 |
|
|
37 |
double latitude,longitude; |
double latitude,longitude; |
38 |
|
|
39 |
|
@SuppressWarnings("unchecked") |
40 |
@Override |
@Override |
41 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
42 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
56 |
String addr = launchedBy.getStringExtra("address"); |
String addr = launchedBy.getStringExtra("address"); |
57 |
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
((TextView) findViewById(R.id.stationAddr)).setText( addr ); |
58 |
|
|
59 |
|
int stationId = launchedBy.getIntExtra("stationid", -1); |
60 |
|
|
61 |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
findViewById(R.id.header).setOnClickListener( mapLauncher ); |
62 |
|
|
63 |
NumberFormat format = NumberFormat.getNumberInstance(); |
NumberFormat format = NumberFormat.getNumberInstance(); |
67 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); |
68 |
|
|
69 |
|
|
|
showDialog(DLG_PROGRESS); |
|
70 |
provider = ProviderFactory.getDepartureProvider(); |
provider = ProviderFactory.getDepartureProvider(); |
71 |
|
|
72 |
fetcher = new DepartureFetcher(); |
fetcher = new DepartureFetcher(); |
73 |
fetcher.execute(name); |
if (savedInstanceState == null) { |
74 |
|
showDialog(DLG_PROGRESS); |
75 |
|
fetcher.execute(stationId); |
76 |
|
} else { |
77 |
|
departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures"); |
78 |
|
adapter.setDepartures(departures); |
79 |
|
selectedItemId = savedInstanceState.getInt("selectedItemId"); |
80 |
|
boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); |
81 |
|
if (detailsShowing) |
82 |
|
showDialog(DLG_DETAILS); |
83 |
|
} |
84 |
} |
} |
85 |
|
|
86 |
@Override |
@Override |
87 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
88 |
{ |
{ |
89 |
if (pgDialog.isShowing()) |
if (pgDialog != null && pgDialog.isShowing()) |
90 |
pgDialog.dismiss(); |
dismissDialog(DLG_PROGRESS); |
91 |
|
boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); |
92 |
|
if (detailsShowing) { |
93 |
|
dismissDialog(DLG_DETAILS); |
94 |
|
} |
95 |
|
outState.putBoolean("detailsShowing", detailsShowing); |
96 |
|
outState.putInt("selectedItemId", selectedItemId); |
97 |
|
|
98 |
|
outState.putSerializable("departures", (ArrayList<DepartureBean>) departures); |
99 |
} |
} |
100 |
|
|
101 |
@Override |
@Override |
102 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
103 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
104 |
|
|
105 |
currentDeparture = departures.get(position); |
selectedItemId = position; |
106 |
showDialog(DLG_DETAILS); |
showDialog(DLG_DETAILS); |
107 |
} |
} |
108 |
|
|
110 |
@Override |
@Override |
111 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
112 |
super.onPrepareDialog(id, dialog); |
super.onPrepareDialog(id, dialog); |
113 |
|
|
114 |
switch (id) { |
switch (id) { |
115 |
case DLG_DETAILS: |
case DLG_DETAILS: |
116 |
|
DepartureBean currentDeparture = departures.get(selectedItemId); |
117 |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
118 |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
119 |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
122 |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
123 |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
124 |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
125 |
|
detailsDialog = dialog; |
126 |
break; |
break; |
127 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
128 |
pgDialog = (ProgressDialog) dialog; |
pgDialog = (ProgressDialog) dialog; |
177 |
}; |
}; |
178 |
|
|
179 |
|
|
180 |
class DepartureFetcher extends AsyncTask<String, Void, Void> { |
class DepartureFetcher extends AsyncTask<Integer, Void, Void> { |
181 |
|
|
182 |
@Override |
@Override |
183 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
191 |
} |
} |
192 |
|
|
193 |
@Override |
@Override |
194 |
protected Void doInBackground(String... params) { |
protected Void doInBackground(Integer... params) { |
195 |
provider.lookupDepartures(params[0]); |
provider.lookupDepartures(params[0]); |
196 |
departures = provider.getDepartures(); |
departures = provider.getDepartures(); |
197 |
return null; |
return null; |