I want to put a generic POJO into ContentValues and unmarshall it within the ContentProvider.
I've been wracking my tiny brain re: Parcelables, ContentValues, and inserting into SQLite Regarding: http://njzk2.wordpress.com/2013/05/31/map-to-contentvalues-abusing-parcelable/
How to write a common code for inserting data in android's Sqlite
I've been trying to insert a android.location.Location into SQLite via ContentProvider:
Location loc = mLocationClient.getLastLocation();
myParcel = android.os.Parcel.obtain();
loc.writeToParcel(myParcel, 0);
ContentValues values = ContentValues.CREATOR.createFromParcel(myParcel );
to populate values w/ parcel.
Question 1) Here is my ContentProvider.insert method:
@Override
public Uri insert( final Uri uri, final ContentValues values ){
SQLiteDatabase db = Mydatabase.getWritableDatabase();
//db.insert() doesn’t unmarshal the values??
db.insert( myTABLE_NAME, “”, values);
Uri result = null;
db.close();
return result;
}
this fails because the db.insert() doesn’t unmarshal the values (i believe) Error inserting android.database.sqlite.SQLiteException: INSERT INTO myTABLE_NAME() VALUES (NULL)
Question 2) Is there some way I can unmarshal values first and then marshal it back into another ContentValues variable? maybe w/ getKey()???
This works:
HashMap hm = new HashMap();
Location loc = mLocationClient.getLastLocation();
hm.put("LOCATIONS", loc);
android.os.Parcel myParcel = android.os.Parcel.obtain();
myParcel.writeMap(hm);
myParcel.setDataPosition(0);
ContentValues values = ContentValues.CREATOR.createFromParcel(myParcel);
getContentResolver().insert(MyUri, values);
and then
@Override
public Uri insert( final Uri uri, final ContentValues oldvalues ){
SQLiteDatabase db = GAELdatabase.getWritableDatabase();
Uri result = null;
Location loc = (Location)oldvalues.get("LOCATIONS");
ContentValues values = new ContentValues();
values.put("ALTITUDE", loc.getAltitude());//meters above sea level
values.put("LATITUDE", loc.getLatitude());
values.put("LONGITUDE", loc.getLongitude());
long rowID = db.insert( "MyTABLE_NAME", "", values);
db.close();
return result;
}