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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 362 by torben, Tue Sep 29 21:30:16 2009 UTC revision 1007 by torben, Tue Aug 3 06:12:10 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3    
4    import static dk.thoerup.traininfo.R.string.generic_cancel;
5    import static dk.thoerup.traininfo.R.string.generic_retry;
6    import static dk.thoerup.traininfo.R.string.timetablelist_fetchdata;
7    import static dk.thoerup.traininfo.R.string.timetablelist_fetcherror;
8    import static dk.thoerup.traininfo.R.string.timetablelist_nodata;
9    
10    import java.util.ArrayList;
11    import java.util.List;
12    
13    import android.app.AlertDialog;
14    import android.app.Dialog;
15  import android.app.ListActivity;  import android.app.ListActivity;
16    import android.app.ProgressDialog;
17    import android.content.DialogInterface;
18    import android.content.Intent;
19    import android.os.AsyncTask;
20  import android.os.Bundle;  import android.os.Bundle;
21    import android.util.Log;
22    import android.view.View;
23    import android.widget.ListView;
24  import android.widget.TextView;  import android.widget.TextView;
25    import dk.thoerup.traininfo.provider.ProviderFactory;
26    import dk.thoerup.traininfo.provider.TimetableProvider;
27    import dk.thoerup.traininfo.util.MessageBox;
28    
29  public class TimetableList extends ListActivity {  public class TimetableList extends ListActivity {
30            
31            private static final int DLG_PROGRESS = 8000;
32            DepartureEntry departure;
33            TimetableListAdapter adapter;
34            TimetableFetcher fetcher;
35            List<TimetableBean> timetables;
36            
37            TimetableProvider provider;
38    
39            @SuppressWarnings("unchecked")
40          @Override          @Override
41          protected void onCreate(Bundle savedInstanceState) {          protected void onCreate(Bundle savedInstanceState) {
42                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
43                  setContentView(R.layout.timetablelist);                  setContentView(R.layout.timetablelist);
44                    
45                    provider = ProviderFactory.getTimetableProvider();
46                    
47                    adapter = new TimetableListAdapter(this);              
48                    setListAdapter(adapter);
49    
50    
51                    
52                    Intent launchedBy = getIntent();
53                    departure = (DepartureEntry) launchedBy.getSerializableExtra("departure");
54                    
55                    ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());
56                    ((TextView)findViewById(R.id.Status)).setText(departure.getStatus());
57                    ((TextView)findViewById(R.id.Location)).setText(departure.getLocation());
58                    ((TextView)findViewById(R.id.Note)).setText(departure.getNote());
59                    ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this));
60                                                    
61                    
62                    if (savedInstanceState == null) {
63                            startTimetableFetcher();
64                    } else {
65                            timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables");
66                            adapter.setTimetable(timetables);
67                    }
68            }
69    
70            @Override
71            protected void onDestroy() {
72                    super.onDestroy();
73                    
74                    if (fetcher != null) {
75                            fetcher.cancel(true);
76                    }
77            }
78            
79        @Override
80            protected void onListItemClick(ListView l, View v, int position, long id) {
81                    super.onListItemClick(l, v, position, id);
82    
83                    TimetableBean tt = timetables.get(position);
84                    
85                    StationBean station = new StationBean();
86                    station.setName( tt.getStation() );
87                    station.setId( tt.getStationId() );
88                    station.setRegional(true);
89                    
90                    Intent intent = new Intent(this, DepartureList.class);
91                    intent.putExtra("stationbean", station);
92                    startActivity(intent);
93                    
94          }          }
95  /*      
96   *              case DLG_DETAILS:  
97                          DepartureBean currentDeparture = departures.get(selectedItemId);  
98                          ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());          @Override
99                          ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());      public void onSaveInstanceState(Bundle outState)
100                          ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());      {          
101                          ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());          dismissDialog(DLG_PROGRESS);
102                          ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());          outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);
103                          ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());      }
104                          ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());  
105                          ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());          
106                          detailsDialog = dialog;          @Override
107            protected void onPrepareDialog(int id, Dialog dialog) {
108                    super.onPrepareDialog(id, dialog);
109                    
110                    switch (id) {
111                    case DLG_PROGRESS:
112                            //pgDialog = (ProgressDialog) dialog;
113                          break;                          break;
114                    }
115            }
116            
117            @Override
118            protected Dialog onCreateDialog(int id) {
119                    switch (id) {
120                    case DLG_PROGRESS:
121                            ProgressDialog dlg = new ProgressDialog(this);                  
122                            dlg.setMessage( getString(timetablelist_fetchdata) );
123                            dlg.setCancelable(true);
124                            return dlg;                    
125                    default:
126                            return super.onCreateDialog(id);                
127                    }
128            }
129    
130   */          void startTimetableFetcher() {
131                    showDialog(DLG_PROGRESS);
132                    fetcher = new TimetableFetcher();
133                    fetcher.execute(departure.getType(), departure.getTrainNumber());
134            }
135            
136            class TimetableFetcher extends AsyncTask<String,Void,Void> {
137                    
138                    
139                    @Override
140                    protected void onPostExecute(Void result) {
141                            super.onPostExecute(result);
142                            dismissDialog(DLG_PROGRESS);
143            
144                            
145                            if (timetables != null) {
146                                    TimetableList.this.getListView().invalidateViews();
147                                    adapter.setTimetable(timetables);
148                                    if (timetables.size() == 0) {
149                                            MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
150                                    }
151                            } else { // communication or parse error
152                                    AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                              
153                                    builder.setMessage(getString(timetablelist_fetcherror));
154                                    builder.setCancelable(true);
155                                    builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
156                                            public void onClick(DialogInterface dialog, int id) {
157                                                    dialog.dismiss();
158                                                    startTimetableFetcher();
159                                                    
160                                            }
161                                    });
162                                    builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
163                                            public void onClick(DialogInterface dialog, int id) {
164                                                    dialog.dismiss();
165                                                    TimetableList.this.finish();
166                                            }                                                      
167                                    });
168                                    
169                                    try {
170                                            builder.show();
171                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
172                                            Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
173                                    }
174                                                                    
175                            }
176                            
177                    }
178                    
179                    @Override
180                    protected Void doInBackground(String... arg0) {
181                            String type = arg0[0];
182                            String trainID = arg0[1];
183                            timetables = provider.lookupTimetable(type, trainID);
184                            
185                            return null;
186                    }
187                    
188            }
189  }  }

Legend:
Removed from v.362  
changed lines
  Added in v.1007

  ViewVC Help
Powered by ViewVC 1.1.20