1 |
package dk.thoerup.traininfo;
|
2 |
|
3 |
import java.text.NumberFormat;
|
4 |
import java.util.List;
|
5 |
|
6 |
import android.app.Dialog;
|
7 |
import android.app.ListActivity;
|
8 |
import android.app.ProgressDialog;
|
9 |
import android.content.Intent;
|
10 |
import android.os.AsyncTask;
|
11 |
import android.os.Bundle;
|
12 |
import android.view.View;
|
13 |
import android.widget.ListView;
|
14 |
import android.widget.TextView;
|
15 |
import dk.thoerup.traininfo.provider.DepartureFactory;
|
16 |
import dk.thoerup.traininfo.provider.DepartureProvider;
|
17 |
|
18 |
public class DepartureList extends ListActivity {
|
19 |
|
20 |
public static final int DLG_PROGRESS = 1;
|
21 |
public static final int DLG_DETAILS = 2;
|
22 |
|
23 |
DepartureListAdapter adapter;
|
24 |
DepartureProvider provider;
|
25 |
List<DepartureBean> departures;
|
26 |
|
27 |
static int itemId;
|
28 |
static DepartureBean currentDeparture;
|
29 |
|
30 |
ProgressDialog pgDialog;
|
31 |
|
32 |
@Override
|
33 |
protected void onCreate(Bundle savedInstanceState) {
|
34 |
super.onCreate(savedInstanceState);
|
35 |
setContentView(R.layout.departurelist);
|
36 |
|
37 |
adapter = new DepartureListAdapter(this);
|
38 |
setListAdapter(adapter);
|
39 |
|
40 |
Intent launchedBy = getIntent();
|
41 |
String name = launchedBy.getStringExtra("name");
|
42 |
((TextView) findViewById(R.id.stationName)).setText( name );
|
43 |
|
44 |
String addr = launchedBy.getStringExtra("address");
|
45 |
((TextView) findViewById(R.id.stationAddr)).setText( addr );
|
46 |
|
47 |
|
48 |
NumberFormat format = NumberFormat.getNumberInstance();
|
49 |
format.setMaximumFractionDigits(1);
|
50 |
format.setMinimumFractionDigits(1);
|
51 |
int distance = launchedBy.getIntExtra("distance", 0);
|
52 |
((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
|
53 |
|
54 |
showDialog(DLG_PROGRESS);
|
55 |
provider = DepartureFactory.getProvider();
|
56 |
provider.lookupDepartures(name);
|
57 |
departures = provider.getDepartures();
|
58 |
adapter.setDepartures(departures);
|
59 |
pgDialog.dismiss();
|
60 |
}
|
61 |
|
62 |
@Override
|
63 |
protected void onListItemClick(ListView l, View v, int position, long id) {
|
64 |
super.onListItemClick(l, v, position, id);
|
65 |
|
66 |
currentDeparture = departures.get(position);
|
67 |
showDialog(DLG_DETAILS);
|
68 |
}
|
69 |
|
70 |
|
71 |
@Override
|
72 |
protected void onPrepareDialog(int id, Dialog dialog) {
|
73 |
super.onPrepareDialog(id, dialog);
|
74 |
|
75 |
switch (id) {
|
76 |
case DLG_DETAILS:
|
77 |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());
|
78 |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());
|
79 |
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());
|
80 |
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());
|
81 |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());
|
82 |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());
|
83 |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());
|
84 |
break;
|
85 |
case DLG_PROGRESS:
|
86 |
pgDialog = (ProgressDialog) dialog;
|
87 |
break;
|
88 |
}
|
89 |
}
|
90 |
|
91 |
@Override
|
92 |
protected Dialog onCreateDialog(int id) {
|
93 |
switch (id) {
|
94 |
case DLG_PROGRESS:
|
95 |
ProgressDialog dlg = new ProgressDialog(this);
|
96 |
dlg.setMessage("Fetch departure data");
|
97 |
dlg.setCancelable(true);
|
98 |
return dlg;
|
99 |
case DLG_DETAILS:
|
100 |
//Context mContext = getApplicationContext();
|
101 |
Dialog dialog = new Dialog(this);
|
102 |
dialog.setCancelable(true);
|
103 |
|
104 |
dialog.setContentView(R.layout.departuredetails);
|
105 |
dialog.setTitle("Departure details");
|
106 |
|
107 |
View root = dialog.findViewById(R.id.layout_root);
|
108 |
root.setOnClickListener( new DialogDismisser(dialog) );
|
109 |
return dialog;
|
110 |
default:
|
111 |
return super.onCreateDialog(id);
|
112 |
}
|
113 |
}
|
114 |
|
115 |
class DialogDismisser implements View.OnClickListener {
|
116 |
|
117 |
Dialog dlg;
|
118 |
public DialogDismisser(Dialog d) {
|
119 |
dlg = d;
|
120 |
}
|
121 |
|
122 |
@Override
|
123 |
public void onClick(View v) {
|
124 |
if (dlg.isShowing())
|
125 |
dlg.dismiss();
|
126 |
}
|
127 |
}
|
128 |
}
|