/[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 365 by torben, Wed Sep 30 09:14:27 2009 UTC revision 981 by torben, Sat Jul 10 16:03: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;  import java.util.ArrayList;
11  import java.util.List;  import java.util.List;
12    
# Line 13  import android.content.Intent; Line 19  import android.content.Intent;
19  import android.os.AsyncTask;  import android.os.AsyncTask;
20  import android.os.Bundle;  import android.os.Bundle;
21  import android.util.Log;  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;  import dk.thoerup.traininfo.provider.ProviderFactory;
26  import dk.thoerup.traininfo.provider.TimetableProvider;  import dk.thoerup.traininfo.provider.TimetableProvider;
# Line 21  import dk.thoerup.traininfo.util.Message Line 29  import dk.thoerup.traininfo.util.Message
29  public class TimetableList extends ListActivity {  public class TimetableList extends ListActivity {
30                    
31          private static final int DLG_PROGRESS = 8000;          private static final int DLG_PROGRESS = 8000;
32          DepartureBean departure;          DepartureEntry departure;
33          TimetableListAdapter adapter;          TimetableListAdapter adapter;
34          TimetableFetcher fetcher;          TimetableFetcher fetcher;
35          List<TimetableBean> timetables;          List<TimetableBean> timetables;
# Line 36  public class TimetableList extends ListA Line 44  public class TimetableList extends ListA
44                                    
45                  provider = ProviderFactory.getTimetableProvider();                  provider = ProviderFactory.getTimetableProvider();
46                                    
47                  adapter = new TimetableListAdapter(this);                  adapter = new TimetableListAdapter(this);              
48                  setListAdapter(adapter);                  setListAdapter(adapter);
49    
50    
51                                    
52                  Intent launchedBy = getIntent();                  Intent launchedBy = getIntent();
53                  departure = (DepartureBean) launchedBy.getSerializableExtra("departure");                  departure = (DepartureEntry) launchedBy.getSerializableExtra("departure");
54                                    
55                  ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());                  ((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber());
56                  ((TextView)findViewById(R.id.Status)).setText(departure.getStatus());                  ((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());                  ((TextView)findViewById(R.id.Note)).setText(departure.getNote());
59                  ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString());                  ((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this));
60                                                                    
61                                    
62                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
63                          startTimetableFetcher();                          startTimetableFetcher();
# Line 55  public class TimetableList extends ListA Line 66  public class TimetableList extends ListA
66                          adapter.setTimetable(timetables);                          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      @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    
97    
98            @Override
99      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
100      {                {          
101          dismissDialog(DLG_PROGRESS);          dismissDialog(DLG_PROGRESS);
102          outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);          outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables);
103      }      }
104    
 /*              case DLG_DETAILS:  
                         DepartureBean currentDeparture = departures.get(selectedItemId);  
                         ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime());  
                         ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber());  
                         ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination());  
                         ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin());  
                         ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation());  
                         ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString());  
                         ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus());  
                         ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote());  
                         detailsDialog = dialog;  
                         break;  
   
  */  
105                    
106          @Override          @Override
107          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
# Line 93  public class TimetableList extends ListA Line 118  public class TimetableList extends ListA
118          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
119                  switch (id) {                  switch (id) {
120                  case DLG_PROGRESS:                  case DLG_PROGRESS:
121                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);                  
122                          dlg.setMessage("Fetch timetable data");                          dlg.setMessage( getString(timetablelist_fetchdata) );
123                          dlg.setCancelable(true);                          dlg.setCancelable(true);
124                          return dlg;                                              return dlg;                    
125                  default:                  default:
# Line 105  public class TimetableList extends ListA Line 130  public class TimetableList extends ListA
130          void startTimetableFetcher() {          void startTimetableFetcher() {
131                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
132                  fetcher = new TimetableFetcher();                  fetcher = new TimetableFetcher();
133                  fetcher.execute(departure.getTrainNumber());                  fetcher.execute(departure.getType(), departure.getTrainNumber());
134          }          }
135                    
136          class TimetableFetcher extends AsyncTask<String,Void,Void> {          class TimetableFetcher extends AsyncTask<String,Void,Void> {
# Line 119  public class TimetableList extends ListA Line 144  public class TimetableList extends ListA
144                    
145                                                    
146                          if (success) {                          if (success) {
147                                    TimetableList.this.getListView().invalidateViews();
148                                  adapter.setTimetable(timetables);                                  adapter.setTimetable(timetables);
149                                  if (timetables.size() == 0) {                                  if (timetables.size() == 0) {
150                                          MessageBox.showMessage(TimetableList.this, "No timetable found");                                          MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true);
151                                  }                                  }
152                          } else { // communication or parse error                          } else { // communication or parse error
153                                  AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                                                                AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this);                                              
154                                  builder.setMessage("Error finding departures");                                  builder.setMessage(getString(timetablelist_fetcherror));
155                                  builder.setCancelable(true);                                  builder.setCancelable(true);
156                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
157                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
158                                                  dialog.dismiss();                                                  dialog.dismiss();
159                                                  startTimetableFetcher();                                                  startTimetableFetcher();
160                                                                                                    
161                                          }                                          }
162                                  });                                  });
163                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
164                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
165                                                  dialog.dismiss();                                                  dialog.dismiss();
166                                                    TimetableList.this.finish();
167                                          }                                                                                                }                                                      
168                                  });                                                                                                                              });
169                                  builder.show();                                                          
170                                    try {
171                                            builder.show();
172                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
173                                            Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
174                                    }
175                                                                    
176                          }                          }
177                                                    
178                  }                  }
179                                    
180                  @Override                  @Override
181                  protected Void doInBackground(String... arg0) {                  protected Void doInBackground(String... arg0) {
182                          String trainID = arg0[0];                          String type = arg0[0];
183                          success = provider.lookupTimetable(trainID);                          String trainID = arg0[1];
184                          timetables = provider.getTimetable();                          success = provider.lookupTimetable(type, trainID);
185                            timetables = provider.getTimetable(type, trainID);
186                                                    
187                          return null;                          return null;
188                  }                  }

Legend:
Removed from v.365  
changed lines
  Added in v.981

  ViewVC Help
Powered by ViewVC 1.1.20