/[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 557 - (hide annotations) (download)
Wed Jan 27 10:04:28 2010 UTC (14 years, 4 months ago) by torben
File size: 5639 byte(s)
Simplify data transfer between these two activities

1 torben 237 package dk.thoerup.traininfo;
2    
3     import java.text.NumberFormat;
4 torben 257 import java.util.ArrayList;
5 torben 237 import java.util.List;
6    
7 torben 336 import android.app.AlertDialog;
8 torben 237 import android.app.Dialog;
9     import android.app.ListActivity;
10     import android.app.ProgressDialog;
11 torben 336 import android.content.DialogInterface;
12 torben 237 import android.content.Intent;
13 torben 239 import android.net.Uri;
14 torben 237 import android.os.AsyncTask;
15     import android.os.Bundle;
16     import android.view.View;
17     import android.widget.ListView;
18     import android.widget.TextView;
19 torben 255 import dk.thoerup.traininfo.provider.DepartureProvider;
20 torben 253 import dk.thoerup.traininfo.provider.ProviderFactory;
21 torben 245 import dk.thoerup.traininfo.util.MessageBox;
22 torben 237
23     public class DepartureList extends ListActivity {
24    
25     public static final int DLG_PROGRESS = 1;
26    
27 torben 362
28 torben 237 DepartureListAdapter adapter;
29     DepartureProvider provider;
30     List<DepartureBean> departures;
31    
32 torben 257 int selectedItemId;
33     //DepartureBean currentDeparture;
34 torben 237
35     ProgressDialog pgDialog;
36 torben 362
37 torben 238 DepartureFetcher fetcher;
38 torben 557
39     StationBean station;
40 torben 238
41 torben 257 @SuppressWarnings("unchecked")
42 torben 237 @Override
43     protected void onCreate(Bundle savedInstanceState) {
44     super.onCreate(savedInstanceState);
45     setContentView(R.layout.departurelist);
46    
47     adapter = new DepartureListAdapter(this);
48     setListAdapter(adapter);
49    
50     Intent launchedBy = getIntent();
51 torben 557
52     station = (StationBean) launchedBy.getSerializableExtra("stationbean");
53 torben 239
54 torben 557 ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
55 torben 317
56 torben 557
57     ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
58 torben 237
59 torben 294
60 torben 239 findViewById(R.id.header).setOnClickListener( mapLauncher );
61 torben 237
62     NumberFormat format = NumberFormat.getNumberInstance();
63     format.setMaximumFractionDigits(1);
64     format.setMinimumFractionDigits(1);
65 torben 557
66    
67     int distance = station.getDistance();
68 torben 237 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
69 torben 238
70 torben 552
71 torben 557 if (station.isRegional() == false && station.isSTrain() == false) {
72 torben 552 getListView().setVisibility( View.GONE );
73     findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
74    
75 torben 257 } else {
76 torben 552 provider = ProviderFactory.getDepartureProvider();
77    
78     if (savedInstanceState == null) {
79     startDepartureFetcher();
80     } else {
81     departures = (List<DepartureBean>) savedInstanceState.getSerializable("departures");
82     adapter.setDepartures(departures);
83     selectedItemId = savedInstanceState.getInt("selectedItemId");
84     }
85 torben 257 }
86 torben 237 }
87    
88 torben 243 @Override
89     public void onSaveInstanceState(Bundle outState)
90     {
91 torben 257 if (pgDialog != null && pgDialog.isShowing())
92     dismissDialog(DLG_PROGRESS);
93 torben 362
94 torben 257 outState.putInt("selectedItemId", selectedItemId);
95    
96     outState.putSerializable("departures", (ArrayList<DepartureBean>) departures);
97 torben 243 }
98    
99 torben 237 @Override
100     protected void onListItemClick(ListView l, View v, int position, long id) {
101     super.onListItemClick(l, v, position, id);
102 torben 362
103     selectedItemId = position;
104 torben 364
105     DepartureBean dep = departures.get(selectedItemId);
106 torben 237
107 torben 362 Intent intent = new Intent(this, TimetableList.class);
108 torben 364 intent.putExtra("departure", dep);
109 torben 362
110     startActivity(intent);
111    
112 torben 237 }
113    
114    
115     @Override
116     protected void onPrepareDialog(int id, Dialog dialog) {
117     super.onPrepareDialog(id, dialog);
118 torben 257
119 torben 237 switch (id) {
120     case DLG_PROGRESS:
121     pgDialog = (ProgressDialog) dialog;
122     break;
123     }
124     }
125    
126     @Override
127     protected Dialog onCreateDialog(int id) {
128     switch (id) {
129     case DLG_PROGRESS:
130     ProgressDialog dlg = new ProgressDialog(this);
131     dlg.setMessage("Fetch departure data");
132     dlg.setCancelable(true);
133 torben 362 return dlg;
134 torben 237 default:
135     return super.onCreateDialog(id);
136     }
137     }
138 torben 336
139     void startDepartureFetcher() {
140     showDialog(DLG_PROGRESS);
141     fetcher = new DepartureFetcher();
142 torben 557 fetcher.execute(station.getId());
143 torben 336 }
144 torben 316
145 torben 237 class DialogDismisser implements View.OnClickListener {
146    
147     Dialog dlg;
148     public DialogDismisser(Dialog d) {
149     dlg = d;
150     }
151    
152     @Override
153     public void onClick(View v) {
154     if (dlg.isShowing())
155     dlg.dismiss();
156 torben 365 }
157     }
158 torben 238
159 torben 239 View.OnClickListener mapLauncher = new View.OnClickListener() {
160     @Override
161 torben 557 public void onClick(View v) {
162     Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
163 torben 239 startActivity( new Intent(Intent.ACTION_VIEW, uri));
164     }
165     };
166 torben 336
167    
168 torben 238
169 torben 310 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
170 torben 238
171 torben 319 boolean success;
172 torben 365
173 torben 238 @Override
174     protected void onPostExecute(Void result) {
175     super.onPostExecute(result);
176    
177 torben 336
178 torben 238 pgDialog.dismiss();
179    
180 torben 319 if (success) {
181 torben 336 adapter.setDepartures(departures);
182 torben 319 if (departures.size() == 0) {
183     MessageBox.showMessage(DepartureList.this, "No departures found");
184     }
185     } else { // communication or parse error
186 torben 336 AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
187     builder.setMessage("Error finding departures");
188     builder.setCancelable(true);
189     builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
190     public void onClick(DialogInterface dialog, int id) {
191     dialog.dismiss();
192     startDepartureFetcher();
193    
194     }
195     });
196     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
197     public void onClick(DialogInterface dialog, int id) {
198     dialog.dismiss();
199     }
200     });
201     builder.show();
202 torben 319 }
203 torben 238 }
204    
205     @Override
206 torben 310 protected Void doInBackground(Integer... params) {
207 torben 319 success = provider.lookupDepartures(params[0]);
208     departures = provider.getDepartures(params[0]);
209 torben 238 return null;
210     }
211    
212     }
213 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20