Skip to content Skip to sidebar Skip to footer

Android Save Object As Blob In Sqlite

is this right. I have this gridview. I want to save its item as blob in sqlite. so when i open the list of save data in sqlite ill just load the save items and call adapter.notifyD

Solution 1:

The way I store data as BLOB in my DB, is by converting them to JSON and then storing the bytes. e.g.

ArrayList<Person> persons  = newArrayList<>();
Gsongson=newGson();
ContentValuesvalues=newContentValues();
values.put(MyProvider.KEY_DATA, gson.toJson(persons).getBytes());
// insert or update the DB

And to get the list back

byte[] blob = cursor.getBlob(cursor.getColumnIndex(MyProvider.KEY_DATA));
Stringjson=newString(blob);
Gsongson=newGson();
ArrayList<Person> persons = gson.fromJson(json, newTypeToken<ArrayList<Person>>()
                                 {}.getType());

Edit: to answer your last question, you should store your data (list of items).

Solution 2:

instead of (Object o), pass (Serializable o). Then, in the stuff you pass, add implements Serializable to class definition.

Post a Comment for "Android Save Object As Blob In Sqlite"