/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/TimetableList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 365 - (hide annotations) (download)
Wed Sep 30 09:14:27 2009 UTC (14 years, 8 months ago) by torben
File size: 4700 byte(s)
First workable version with timetable feature
1 torben 362 package dk.thoerup.traininfo;
2    
3 torben 365
4     import java.util.ArrayList;
5     import java.util.List;
6    
7     import android.app.AlertDialog;
8     import android.app.Dialog;
9 torben 362 import android.app.ListActivity;
10 torben 365 import android.app.ProgressDialog;
11     import android.content.DialogInterface;
12 torben 364 import android.content.Intent;
13 torben 365 import android.os.AsyncTask;
14 torben 362 import android.os.Bundle;
15 torben 365 import android.util.Log;
16 torben 362 import android.widget.TextView;
17 torben 365 import dk.thoerup.traininfo.provider.ProviderFactory;
18     import dk.thoerup.traininfo.provider.TimetableProvider;
19     import dk.thoerup.traininfo.util.MessageBox;
20 torben 362
21     public class TimetableList extends ListActivity {
22 torben 364
23 torben 365 private static final int DLG_PROGRESS = 8000;
24 torben 364 DepartureBean departure;
25 torben 365 TimetableListAdapter adapter;
26     TimetableFetcher fetcher;
27     List<TimetableBean> timetables;
28 torben 364
29 torben 365 TimetableProvider provider;
30    
31     @SuppressWarnings("unchecked")
32 torben 362 @Override
33     protected void onCreate(Bundle savedInstanceState) {
34     super.onCreate(savedInstanceState);
35     setContentView(R.layout.timetablelist);
36 torben 364
37 torben 365 provider = ProviderFactory.getTimetableProvider();
38    
39     adapter = new TimetableListAdapter(this);
40     setListAdapter(adapter);
41    
42 torben 364 Intent launchedBy = getIntent();
43     departure = (DepartureBean) launchedBy.getSerializableExtra("departure");
44    
45     ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());
46     ((TextView)findViewById(R.id.Status)).setText(departure.getStatus());
47     ((TextView)findViewById(R.id.Note)).setText(departure.getNote());
48     ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString());
49 torben 365
50    
51     if (savedInstanceState == null) {
52     startTimetableFetcher();
53     } else {
54     timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables");
55     adapter.setTimetable(timetables);
56     }
57 torben 362 }
58 torben 365
59     @Override
60     public void onSaveInstanceState(Bundle outState)
61     {
62     dismissDialog(DLG_PROGRESS);
63     outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);
64     }
65 torben 364
66     /* case DLG_DETAILS:
67 torben 362 DepartureBean currentDeparture = departures.get(selectedItemId);
68     ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());
69     ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());
70     ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());
71     ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());
72     ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());
73     ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());
74     ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());
75     ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());
76     detailsDialog = dialog;
77     break;
78    
79     */
80 torben 365
81     @Override
82     protected void onPrepareDialog(int id, Dialog dialog) {
83     super.onPrepareDialog(id, dialog);
84    
85     switch (id) {
86     case DLG_PROGRESS:
87     //pgDialog = (ProgressDialog) dialog;
88     break;
89     }
90     }
91    
92     @Override
93     protected Dialog onCreateDialog(int id) {
94     switch (id) {
95     case DLG_PROGRESS:
96     ProgressDialog dlg = new ProgressDialog(this);
97     dlg.setMessage("Fetch timetable data");
98     dlg.setCancelable(true);
99     return dlg;
100     default:
101     return super.onCreateDialog(id);
102     }
103     }
104    
105     void startTimetableFetcher() {
106     showDialog(DLG_PROGRESS);
107     fetcher = new TimetableFetcher();
108     fetcher.execute(departure.getTrainNumber());
109     }
110    
111     class TimetableFetcher extends AsyncTask<String,Void,Void> {
112    
113     boolean success;
114    
115     @Override
116     protected void onPostExecute(Void result) {
117     super.onPostExecute(result);
118     dismissDialog(DLG_PROGRESS);
119    
120    
121     if (success) {
122     adapter.setTimetable(timetables);
123     if (timetables.size() == 0) {
124     MessageBox.showMessage(TimetableList.this, "No timetable found");
125     }
126     } else { // communication or parse error
127     AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);
128     builder.setMessage("Error finding departures");
129     builder.setCancelable(true);
130     builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
131     public void onClick(DialogInterface dialog, int id) {
132     dialog.dismiss();
133     startTimetableFetcher();
134    
135     }
136     });
137     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
138     public void onClick(DialogInterface dialog, int id) {
139     dialog.dismiss();
140     }
141     });
142     builder.show();
143     }
144    
145     }
146    
147     @Override
148     protected Void doInBackground(String... arg0) {
149     String trainID = arg0[0];
150     success = provider.lookupTimetable(trainID);
151     timetables = provider.getTimetable();
152    
153     return null;
154     }
155    
156     }
157 torben 362 }

  ViewVC Help
Powered by ViewVC 1.1.20