/[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 398 - (hide annotations) (download)
Tue Oct 6 09:42:10 2009 UTC (14 years, 7 months ago) by torben
File size: 4841 byte(s)
Current Location can still be interesting for some trains

1 torben 362 package dk.thoerup.traininfo;
2    
3 torben 365
4     import java.util.ArrayList;
5     import java.util.List;
6    
7 torben 366 import android.app.Activity;
8 torben 365 import android.app.AlertDialog;
9     import android.app.Dialog;
10     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 366 import android.widget.ListView;
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 torben 366 public class TimetableList extends Activity {
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    
41 torben 366 ListView lv = (ListView) findViewById(R.id.List);
42     lv.setAdapter(adapter);
43    
44    
45 torben 364 Intent launchedBy = getIntent();
46     departure = (DepartureBean) launchedBy.getSerializableExtra("departure");
47    
48     ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());
49     ((TextView)findViewById(R.id.Status)).setText(departure.getStatus());
50 torben 398 ((TextView)findViewById(R.id.Location)).setText(departure.getLocation());
51 torben 364 ((TextView)findViewById(R.id.Note)).setText(departure.getNote());
52     ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString());
53 torben 366
54 torben 365
55     if (savedInstanceState == null) {
56     startTimetableFetcher();
57     } else {
58     timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables");
59     adapter.setTimetable(timetables);
60     }
61 torben 362 }
62 torben 365
63     @Override
64     public void onSaveInstanceState(Bundle outState)
65     {
66     dismissDialog(DLG_PROGRESS);
67     outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);
68     }
69 torben 364
70     /* case DLG_DETAILS:
71 torben 362 DepartureBean currentDeparture = departures.get(selectedItemId);
72     ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());
73     ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());
74     ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());
75     ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());
76     ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());
77     ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());
78     ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());
79     ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());
80     detailsDialog = dialog;
81     break;
82    
83     */
84 torben 365
85     @Override
86     protected void onPrepareDialog(int id, Dialog dialog) {
87     super.onPrepareDialog(id, dialog);
88    
89     switch (id) {
90     case DLG_PROGRESS:
91     //pgDialog = (ProgressDialog) dialog;
92     break;
93     }
94     }
95    
96     @Override
97     protected Dialog onCreateDialog(int id) {
98     switch (id) {
99     case DLG_PROGRESS:
100     ProgressDialog dlg = new ProgressDialog(this);
101     dlg.setMessage("Fetch timetable data");
102     dlg.setCancelable(true);
103     return dlg;
104     default:
105     return super.onCreateDialog(id);
106     }
107     }
108    
109     void startTimetableFetcher() {
110     showDialog(DLG_PROGRESS);
111     fetcher = new TimetableFetcher();
112     fetcher.execute(departure.getTrainNumber());
113     }
114    
115     class TimetableFetcher extends AsyncTask<String,Void,Void> {
116    
117     boolean success;
118    
119     @Override
120     protected void onPostExecute(Void result) {
121     super.onPostExecute(result);
122     dismissDialog(DLG_PROGRESS);
123    
124    
125     if (success) {
126     adapter.setTimetable(timetables);
127     if (timetables.size() == 0) {
128     MessageBox.showMessage(TimetableList.this, "No timetable found");
129     }
130     } else { // communication or parse error
131     AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);
132     builder.setMessage("Error finding departures");
133     builder.setCancelable(true);
134     builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
135     public void onClick(DialogInterface dialog, int id) {
136     dialog.dismiss();
137     startTimetableFetcher();
138    
139     }
140     });
141     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
142     public void onClick(DialogInterface dialog, int id) {
143     dialog.dismiss();
144     }
145     });
146     builder.show();
147     }
148    
149     }
150    
151     @Override
152     protected Void doInBackground(String... arg0) {
153     String trainID = arg0[0];
154     success = provider.lookupTimetable(trainID);
155 torben 391 timetables = provider.getTimetable(trainID);
156 torben 365
157     return null;
158     }
159    
160     }
161 torben 362 }

  ViewVC Help
Powered by ViewVC 1.1.20