/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Try with DB mBusDbHelper = new BusDbAdapter(this); mBusDbHelper.open(); listType = getIntent().getIntExtra("listType", 0); if (listType == 0) { Toast.makeText(this, "Internal error", Toast.LENGTH_LONG); Log.v(TAG, "Unable to get listType"); finish(); } String[] args = {"routeID", "stopName"}; int[] resources = {R.id.listItemRouteID, R.id.listItemName}; listAdapter = new SimpleAdapter(this, locationList, R.layout.list_item, args, resources); setListAdapter(listAdapter); ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(view.getContext(), ConfirmationPage.class); HashMap<String, String> item = locationList.get(position); DataFetcher df = new DataFetcher(); try { BusStop b = df.getStopById(Integer.parseInt(item.get("stopID").split("_")[1])); i.putExtra("busstop", b); i.putExtra("busroute", item.get("routeID")); i.putExtra("busroutedesc", item.get("routeDesc")); startActivity(i); finish(); // if an exception occurs, nothing happens (for now). } catch (NumberFormatException e) { Log.v(TAG, "Error parsing stop id!"); e.printStackTrace(); } catch (IOException e) { Log.v(TAG, "Error fetching info!"); e.printStackTrace(); } } }); registerForContextMenu(getListView()); // populate list items fillList(mBusDbHelper); mBusDbHelper.close(); }
/** actions for the context menu */ @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); int id = (int) getListAdapter().getItemId(info.position); // sets an alarm for the selected stop switch (item.getItemId()) { case SET_STOP_OPTION: Intent i = new Intent(getApplicationContext(), ConfirmationPage.class); HashMap<String, String> busItem = locationList.get(id); DataFetcher df = new DataFetcher(); try { BusStop b = df.getStopById(Integer.parseInt(busItem.get("stopID").split("_")[1])); i.putExtra("busstop", b); i.putExtra("busroute", busItem.get("routeID")); i.putExtra("busroutedesc", busItem.get("routeDesc")); startActivity(i); finish(); // if an exception occurs, nothing happens (for now). } catch (NumberFormatException e) { Log.v(TAG, "Error parsing stop id!"); e.printStackTrace(); } catch (IOException e) { Log.v(TAG, "Error fetching info!"); e.printStackTrace(); } break; // removes the selected stop from the list case REMOVE_STOP_OPTION: HashMap<String, String> itemToRemove = locationList.get(id); mBusDbHelper.open(); mBusDbHelper.deleteDest(itemToRemove.get("routeID"), itemToRemove.get("stopID")); mBusDbHelper.close(); locationList.remove(id); listAdapter.notifyDataSetChanged(); break; case CANCEL: break; default: break; } return true; }