lundi 4 janvier 2016

Cursor with same query returning different result

I am saving a marker in SQLite with this method :

 public long addMarker(Markers marker) {
    SQLiteDatabase db=null;

        ContentValues values = new ContentValues();
        values.put(COLUMN_ADDRESS, marker.get_address());
        values.put(COLUMN_COUNTRY, marker.get_country());
        values.put(COLUMN_LAT, marker.get_lat());
        values.put(COLUMN_LNG, marker.get_lng());
        values.put(COLUMN_IMAGE, marker.get_image());
        db = getWritableDatabase();
        long result = db.insert(TABLE_LOCATIONS, null, values);
        if (result>0){
            db.close();
        }
        return result;
}

then onMarkerClick i must get all the info for the marker i clicked using LatLng for arguments. This is the method i use:

public Markers getOneMarker(double lat, double lng) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor curs = db.query(true,TABLE_LOCATIONS,new String[]{COLUMN_ID,
            COLUMN_ADDRESS,COLUMN_COUNTRY,
            COLUMN_LAT,COLUMN_LNG,COLUMN_IMAGE},
            COLUMN_LAT+"="+lat+" AND "+COLUMN_LNG+"="+lng,
            null,null,null,null,null);
    Markers marker = null;
    if (curs != null) {
        curs.moveToFirst();
        try{
            marker = new Markers(Integer.valueOf(curs.getString(0)),curs.getString(1), curs.getString(2),Double.valueOf(curs.getString(3)),Double.valueOf(curs.getString(4)),curs.getString(5));
        }catch (CursorIndexOutOfBoundsException e){
            e.printStackTrace();
            marker = new Markers("Address for location unavailable ", "N/A", lat, lng, "image");
        }

    }
    assert curs != null;
    curs.close();
    db.close();
    return marker;
}

90% of the times i got what i want(address and country name) but the other 10% i get "Address for location unavailable ", "N/A". I made a Toast in the onClick()to check the Geocoder but its ok. I am getting an address but not from the db. Any ideas?

Aucun commentaire:

Enregistrer un commentaire