/[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 245 - (hide annotations) (download)
Sun Aug 9 19:40:05 2009 UTC (14 years, 9 months ago) by torben
File size: 5091 byte(s)
Small mods to the messagebox
1 torben 237 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 torben 239 import android.net.Uri;
11 torben 237 import android.os.AsyncTask;
12     import android.os.Bundle;
13     import android.view.View;
14     import android.widget.ListView;
15     import android.widget.TextView;
16 torben 238 import android.widget.Toast;
17 torben 237 import dk.thoerup.traininfo.provider.DepartureFactory;
18     import dk.thoerup.traininfo.provider.DepartureProvider;
19 torben 245 import dk.thoerup.traininfo.util.MessageBox;
20 torben 237
21     public class DepartureList extends ListActivity {
22    
23     public static final int DLG_PROGRESS = 1;
24     public static final int DLG_DETAILS = 2;
25    
26     DepartureListAdapter adapter;
27     DepartureProvider provider;
28     List<DepartureBean> departures;
29    
30     static int itemId;
31     static DepartureBean currentDeparture;
32    
33     ProgressDialog pgDialog;
34    
35 torben 238 DepartureFetcher fetcher;
36    
37 torben 239 double latitude,longitude;
38    
39 torben 237 @Override
40     protected void onCreate(Bundle savedInstanceState) {
41     super.onCreate(savedInstanceState);
42     setContentView(R.layout.departurelist);
43    
44     adapter = new DepartureListAdapter(this);
45     setListAdapter(adapter);
46    
47     Intent launchedBy = getIntent();
48 torben 239
49     latitude = launchedBy.getDoubleExtra("latitude", 0.0);
50     longitude = launchedBy.getDoubleExtra("longitude", 0.0);
51    
52 torben 237 String name = launchedBy.getStringExtra("name");
53     ((TextView) findViewById(R.id.stationName)).setText( name );
54    
55     String addr = launchedBy.getStringExtra("address");
56     ((TextView) findViewById(R.id.stationAddr)).setText( addr );
57    
58 torben 239 findViewById(R.id.header).setOnClickListener( mapLauncher );
59 torben 237
60     NumberFormat format = NumberFormat.getNumberInstance();
61     format.setMaximumFractionDigits(1);
62     format.setMinimumFractionDigits(1);
63     int distance = launchedBy.getIntExtra("distance", 0);
64     ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
65    
66 torben 238
67 torben 237 showDialog(DLG_PROGRESS);
68     provider = DepartureFactory.getProvider();
69 torben 238
70     fetcher = new DepartureFetcher();
71     fetcher.execute(name);
72 torben 237 }
73    
74 torben 243 @Override
75     public void onSaveInstanceState(Bundle outState)
76     {
77     if (pgDialog.isShowing())
78     pgDialog.dismiss();
79     }
80    
81 torben 237 @Override
82     protected void onListItemClick(ListView l, View v, int position, long id) {
83     super.onListItemClick(l, v, position, id);
84    
85     currentDeparture = departures.get(position);
86     showDialog(DLG_DETAILS);
87     }
88    
89    
90     @Override
91     protected void onPrepareDialog(int id, Dialog dialog) {
92     super.onPrepareDialog(id, dialog);
93    
94     switch (id) {
95     case DLG_DETAILS:
96     ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());
97     ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());
98     ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());
99     ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());
100     ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());
101     ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());
102     ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());
103     break;
104     case DLG_PROGRESS:
105     pgDialog = (ProgressDialog) dialog;
106     break;
107     }
108     }
109    
110     @Override
111     protected Dialog onCreateDialog(int id) {
112     switch (id) {
113     case DLG_PROGRESS:
114     ProgressDialog dlg = new ProgressDialog(this);
115     dlg.setMessage("Fetch departure data");
116     dlg.setCancelable(true);
117     return dlg;
118     case DLG_DETAILS:
119     //Context mContext = getApplicationContext();
120     Dialog dialog = new Dialog(this);
121     dialog.setCancelable(true);
122    
123     dialog.setContentView(R.layout.departuredetails);
124     dialog.setTitle("Departure details");
125    
126     View root = dialog.findViewById(R.id.layout_root);
127     root.setOnClickListener( new DialogDismisser(dialog) );
128     return dialog;
129     default:
130     return super.onCreateDialog(id);
131     }
132     }
133    
134     class DialogDismisser implements View.OnClickListener {
135    
136     Dialog dlg;
137     public DialogDismisser(Dialog d) {
138     dlg = d;
139     }
140    
141     @Override
142     public void onClick(View v) {
143     if (dlg.isShowing())
144     dlg.dismiss();
145     }
146     }
147 torben 238
148 torben 239 View.OnClickListener mapLauncher = new View.OnClickListener() {
149     @Override
150     public void onClick(View v) {
151     Uri uri = Uri.parse("geo:" + latitude + "," + longitude);
152     startActivity( new Intent(Intent.ACTION_VIEW, uri));
153     }
154     };
155 torben 238
156 torben 239
157 torben 238 class DepartureFetcher extends AsyncTask<String, Void, Void> {
158    
159     @Override
160     protected void onPostExecute(Void result) {
161     super.onPostExecute(result);
162    
163     adapter.setDepartures(departures);
164     pgDialog.dismiss();
165    
166     if (departures.size() == 0)
167 torben 245 MessageBox.showMessage(DepartureList.this, "No departures found");
168 torben 238 }
169    
170     @Override
171     protected Void doInBackground(String... params) {
172     provider.lookupDepartures(params[0]);
173     departures = provider.getDepartures();
174     return null;
175     }
176    
177     }
178 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20