0

My app allows a user to tape on a cross hair in the center of the map and then add a marker. The marker lat, lng as well as name are saved to a database and thats working fine. I dont add an id as my database adds the id.

public long addEntry(String theLabel, String theArea, String theLat, String theLng) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_NAME, theLabel);
    contentValues.put(KEY_AREA, theArea);
    contentValues.put(KEY_LAT, theLat);
    contentValues.put(KEY_LNG, theLng);
    return mySpotsDatabase.insert(DATABASE_TABLE, null, contentValues);

}

All good so far and on subsequent loads of the map I get the data from the SQLite database and populate the map with the markers. When I do the retrieval I also include the id of the SQlite entry.

public Cursor getAllData() {
    String [] columns = new String [] {KEY_ROWID, KEY_NAME, KEY_AREA, KEY_LAT, KEY_LNG};
    Cursor c = mySpotsDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    c.moveToFirst();
    return c;
}

And in my maps page

 MySpotsDatabase spotDatabase = new MySpotsDatabase(MyMap.this);
        spotDatabase.open();
        if (spotDatabase.getNumRows() == 0)
            spotDatabase.addEntry("MarkerTitle", "One", myLatitude.toString(), myLongitude.toString());
        spotDatabase.close();

        spotDatabase.open();
        myDataC = spotDatabase.getAllData();
        do {
            dataprovider = new MarkersDataProvider(myDataC.getString(0), myDataC.getString(1), myDataC.getString(2), myDataC.getString(3), myDataC.getString(4));
            arrayList.add(dataprovider);
        } while (myDataC.moveToNext());
        spotDatabase.close();

Again all good so far, BUT now I want to update the marker if the user drags it to another location or if they want to update the title label.

HOW do I set, get and restore a individual marker and write its updated lat, lng, and name back to the SQLite database. THE WRITING BACK to the SQLite database is trivial and I have that sorted, but I need to know which entry in the SQLite database I need to update.

SO I am trying to find a way to grab the _id that I read from the database for each marker and keep that reference with its marker so I can update the database when needed.

ATTEMPT I tried was setting the marker title to the _id but that gives me a null reference for some reason

LatLng newMarker = new LatLng(myLat, myLng);
                        MarkerOptions newOptions = new MarkerOptions();
                        newOptions.position(newMarker);
                        int lastEntry = arrayList.size()-1;
                        String rowID = arrayList.get(lastEntry).getRowID();
                        newOptions.title(rowID);
                        newOptions.draggable(true);
                        String theSnippet = "Marker id: " + arrayList.get(lastEntry).getRowID() + "\nLat: " + String.valueOf(myLat) + "\nLng: " + String.valueOf(myLng);
                        newOptions.snippet(theSnippet);
                        myMap.addMarker(newOptions);
                        Snackbar.make(findViewById(R.id.myMapCoordinatorLayout), rowID + " " + beforeCount + " " + afterCount , Snackbar.LENGTH_INDEFINITE).show();

Looking at hashmaps but unsure if thats the right solution. I will keep digging as maybe storing the _id in the title might still work if I can sort out why its giving me a null reference.

timv
  • 3,346
  • 4
  • 34
  • 43

1 Answers1

0

SO I have solved the issue of the null reference with GrAnd's answer.

Get gererated ID from inserted row

Here is my line I amended to get the row id:

int newRowID = (int) addToDB.addEntry("New Marker", "One", (String.valueOf(myLat)), (String.valueOf(myLng)));

Still unsure if storing the ROWID in the title is the best way ahead and if I find a better solution I will update my answer.

I also worked out that my dataprovider class was not returning the string value of rowid so it was a rookie mistake on my part.

public class MarkersDataProvider {

private String rowID;
private String name;
private String area; // not used but will be later on.  Just set to "one" for now
private String lat;
private String lng;

public MarkersDataProvider(String rowID, String name, String area, String lat, String lng)
{
    this.setRowID(rowID);
    this.setName(name);
    this.setArea(area);
    this.setLat(lat);
    this.setLng(lng);
}

public String getRowID()
{
    return rowID;
}
public String getName()
{
    return name;
}
public String getArea()
{
    return area;
}
public String getLat()
{
    return lat;
}
public String getLng()
{
    return lng;
}


// never set the rowID as it is generated by the database and I only read it so
// I can reference it in updating and deleting markers

public void setRowID(String rowID)
{
    this.rowID = rowID;
}
public void setName(String name)
{
    this.name = name;
}
public void setArea(String area)
{
    this.area = area;
}
public void setLat(String lat)
{
    this.lat = lat;
}
public void setLng(String lng)
{
    this.lng = lng;
}

SO my plan is to not show the marker title in the info window so that the title can store the row id.

timv
  • 3,346
  • 4
  • 34
  • 43