/**
   * fills the list with stops from the local database
   *
   * @param db the database adapter to use
   */
  private void fillList(BusDbAdapter db) {
    Cursor c;
    if (listType == FAVORITES) {
      c = db.getFavoriteDest(NUM_ENTRIES_TO_FETCH);
    } else { // listType == MAJOR
      c = db.getMajorDest(NUM_ENTRIES_TO_FETCH);
    }
    int stopIDIndex = c.getColumnIndex("stop_id");
    int stopDescIndex = c.getColumnIndex("stop_desc");
    int routeIDIndex = c.getColumnIndex("route_id");
    int routeDescIndex = c.getColumnIndex("route_desc");
    if (c != null) {
      for (int i = 0; i < c.getCount(); i++) {
        HashMap<String, String> item = new HashMap<String, String>();

        String stopID = c.getString(stopIDIndex);
        String stopName = c.getString(stopDescIndex);
        String route = c.getString(routeIDIndex);
        String routeDesc = c.getString(routeDescIndex);
        Log.v(TAG, "PUT");
        Log.v(TAG, "stopID " + stopID + " stopName " + stopName);
        Log.v(TAG, "routeID " + route + " routeDesc" + routeDesc);
        item.put("stopID", stopID);
        item.put("stopName", stopName);
        item.put("routeID", route);
        item.put("routeDesc", routeDesc);
        c.moveToNext();
        locationList.add(item);
      }
      listAdapter.notifyDataSetChanged();
    }
  }
  /** 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;
  }