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()???
insert
, you'll see that the values from theContentValues
are passed tobindOjectToProgram
, which is the last step of building the query. In this method, the type of the object is tested.Location
is not a known type, which means the data in the table end up being thetoString
representation. – Elenaelenchus