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

  ViewVC Help
Powered by ViewVC 1.1.20