jeudi 14 janvier 2016

Values saved in DB return different

This is a Google Map App. onLongClick() marker is addded into two Lists. One is only with LatLng for future save in sharedPrefferences and the other one is being inserted into SQLite in onDestroy(). The problem is, that the LatLng i get back from the database is shorter in leght from that i have in the marker when i put it on the map. This makes my info activity to fail opening. This is my methods involved:

@Override
public void onMapLongClick(LatLng latLng) {
    MarkerOptions markerOptions = new MarkerOptions().position(latLng);
    mMap.addMarker(markerOptions);
    markerOptions=getAddress(markerOptions);
    locations.add(latLng.latitude + ", " + latLng.longitude);
    databaseList.add(markerOptions);
}

from here markerOptions object goes to List<MarkerOptions> databaseList and in onDestroy() the list goes to DB:

public void insertList(List<MarkerOptions> list) {
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_LOCATIONS);
    for (MarkerOptions marker : list) {
        ContentValues values = new ContentValues();
        String[] split = marker.getTitle().split(", ");
        values.put(COLUMN_ADDRESS, split[0]);
        values.put(COLUMN_COUNTRY, split[1]);
        values.put(COLUMN_LAT, marker.getPosition().latitude);
        values.put(COLUMN_LNG, marker.getPosition().longitude);
        db.insertWithOnConflict(TABLE_LOCATIONS, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        values.clear();
    }
    db.close();
}

Then in onCreate() i fill the same list with markerOptions:

public class DatabaseProcessThread extends AsyncTask<List<MarkerOptions>,Void,List<MarkerOptions>>{


    @Override
    protected List<MarkerOptions> doInBackground(List<MarkerOptions>... params) {
        SQLiteHelper db = new SQLiteHelper(getApplicationContext(),null,null,1);
        return db.getExistingMarkers();
    }

    @Override
    protected void onPostExecute(List<MarkerOptions> markerOptionses) {
        super.onPostExecute(markerOptionses);
        databaseList=markerOptionses;
    }
}

The problem is when i tested it with 4 markers, restarted the app and put one more on the map, the old 4 does not open cause they don't pass this if:

 @Override
public boolean onMarkerClick(Marker marker) {

    for (MarkerOptions options : databaseList) {
        if (options.getPosition().latitude==marker.getPosition().latitude&&options.getPosition().longitude==marker.getPosition().longitude){
            Intent intent = new Intent(this,com.example.fixxxer.map.MarkerActivity.class);
            Log.v(TAG,options.getPosition().toString());
            String[]split = options.getTitle().split(", ");
            intent.putExtra("Address",split[0]);
            intent.putExtra("Country",split[1]);
            intent.putExtra("Latitude",marker.getPosition().latitude);
            intent.putExtra("Longitude",marker.getPosition().longitude);
            Toast.makeText(MapsActivity.this,split[0]+","+split[1], Toast.LENGTH_SHORT).show();
            startActivity(intent);
        }else{
            Log.v(TAG,options.getPosition().toString());
        }
    }
    return true;
}

This is how i obtain the markerOptions:

public List<MarkerOptions> getExistingMarkers() {

    ArrayList<MarkerOptions> list = new ArrayList<>();
    String query = "SELECT * FROM " + TABLE_LOCATIONS;
    Cursor curs = null;SQLiteDatabase db = null;
    try {
        db = getReadableDatabase();
        curs = db.rawQuery(query, null);
        curs.moveToFirst();
        if (curs.isFirst()) {
            do {
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.title(curs.getString(1) + ", " + curs.getString(2));
                LatLng latlng = new LatLng(Double.valueOf(curs.getString(3)), Double.valueOf(curs.getString(4)));
                markerOptions.position(latlng);
                list.add(markerOptions);
                curs.moveToNext();
            } while (!curs.isAfterLast());
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    curs.close();
    db.close();
    return list;
}

Log.v on the all 5 locations returned: 01-14 13:21:17.859 7841-7841/com.example.fixxxer.map V/MAPS ACTIVITY: lat/lng: (-16.9619,20.079) 01-14 13:21:17.859 7841-7841/com.example.fixxxer.map V/MAPS ACTIVITY: lat/lng: (-25.4685,23.4743) 01-14 13:21:17.859 7841-7841/com.example.fixxxer.map V/MAPS ACTIVITY: lat/lng: (1.3249,12.0056) 01-14 13:21:17.859 7841-7841/com.example.fixxxer.map V/MAPS ACTIVITY: lat/lng: (-29.0083,24.7622) 01-14 13:21:17.859 7841-7841/com.example.fixxxer.map V/MAPS ACTIVITY: lat/lng: (-4.444019301843549,27.22086824476719) What am i doing wrong?

Aucun commentaire:

Enregistrer un commentaire