mercredi 22 juillet 2015

Loading markers from the database causes their IDs to be reset each time, how can I force them to take on my database IDs?

I'm having a problem with my database and loading markers in my Maps activity. Right now, I have a database holding MapsMarkers (my own class of marker) and a column called "marker_string_id" which holds the ID of the Google markers, which are generated as following: m0, m1, m2, etc. I use this to delete my MapsMarkers, as I delete by the Google marker id (marker_string_id). This works great until the user decides to create more markers and delete more markers. Each time the application is reloaded, the Google markers' IDs are reset, but the MapsMarker marker_string_id's are not. Ex. You have 3 markers, with ID m0, m1, m2. Delete one and reload the application, now you have 2 markers with ID m0, m1 but their IDs in the database are m1, m2. Deleting them no longer works. How can I make it so that the markers load with the correct ID?

Here is my method in my maps activity for loading the markers:

/**
 * Method that adds the actual Google marker to the map in loadmarkers
 * @param lat
 */
private void loadMarkersAdd(LatLng lat) {

        Marker uMarker = Map.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) //Blue marker
                .position(lat) //Places marker at hold area
                .snippet(description));
        String acc = uMarker.getId();
        Log.i(LOGTAG, "New marker ID is " + acc);

}

/**
 * Method for loading the markers onto the map in onResume, onCreate
 */
private void loadMarkers() {
    try {
        databaseHelper.open();
    } catch (Exception e) {
        Log.i(LOGTAG, "Loadmarkers DBOpen failed");
    }

    List<MapsMarker> markersList = databaseHelper.getAllMarkers();
    for(int i = 0; i < markersList.size(); i ++) {
        LatLng lat = new LatLng(markersList.get(i).getLat(), markersList.get(i).getLon());
        String userMarkerIds = markersList.get(i).getId();
            if (userMarkerLatLng.contains(lat) || userMarkerIDs.contains(userMarkerIds)) {
                Log.i(LOGTAG, "Breaking contains");
                break;
            } else {
                Log.i(LOGTAG, "Adding google maps marker");

                loadMarkersAdd(lat); //Loads google maps markers if the marker isnt already there, prevents multiple google markers from being added
            }
        Log.i(LOGTAG, "LoadMarks looping");
    }
}

My method for getting all markers from the database (used when loading markers):

/**
 * Returns a list of all markers currently in the database
 *
 * @return
 */
public List<MapsMarker> getAllMarkers() {      //Cursor is basically the "pointer" where the database is looking at on the table at a given moment
    List<MapsMarker> markers = new ArrayList<>();

    try {
        open();
    } catch (Exception e) {
        Log.i(LOGTAG, "Database opening failed");
    }

    Cursor cursor = db.query(TABLE_LOCATION, allMarkerColumns,
            null, null, null, null, null);
    cursor.moveToFirst();
    Log.i(LOGTAG, "Returned " + cursor.getCount() + " marker table rows");
    if (cursor.getCount() > 0) {
        Log.i(LOGTAG, "GetAllMarkers if statement was run");
        while (!cursor.isAfterLast()) { //while successfully moves to new row in table, return true
            Log.i(LOGTAG, "While loop running");
            MapsMarker marker = new MapsMarker(null, null, null);
            marker.setId(cursor.getString(cursor.getColumnIndex(COLUMN_MARKER_ID)));
            marker.setLat(cursor.getDouble(cursor.getColumnIndex(COLUMN_LAT)));
            marker.setLon(cursor.getDouble(cursor.getColumnIndex(COLUMN_LONGITUDE)));
            markers.add(marker);
            cursor.moveToNext();
        }
    }
    Log.i(LOGTAG, "Closing getAllMarkers cursor");
    cursor.close(); //frees up cursor
    close();
    return markers;

}

And finally, my save and delete methods:

/**
 * Method for adding a marker to the database and returning its ID if needed (for deletion, find, etc.)
 * @return
 */
public void saveMapsMarker(MapsMarker marker) {
    open();
    ContentValues values = new ContentValues(); //Dont .put COLUMN_MARKER_ID because it is autoincrementing
    values.put(COLUMN_MARKER_STRING_ID, marker.getId());
    values.put(COLUMN_LAT, marker.getLat());
    values.put(COLUMN_LONGITUDE, marker.getLon());

    db.insert(TABLE_LOCATION, null, values);
    close();
}

/**
 * Method for deleting marker by ID
 *
 * @param id
 */
public void deleteMapsMarker(String id) {
    open();
    db.delete(TABLE_LOCATION,
            COLUMN_MARKER_STRING_ID + "=?", new String[]{String.valueOf(id)});
    close();
}

Aucun commentaire:

Enregistrer un commentaire