这是正确的吗。我有这个gridview
。我想将其项目另存为sqlite中的blob。所以当我在sqlite病中打开保存数据列表时,只需加载保存items
并调用adapter.notifyDataSetChanged
我的gridview
我尝试了这个但是我得到了NotSerializableException
错误
列表项= new ArrayList();
public static byte[] serializeObject(Object o) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(o);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
return buf;
} catch (IOException ioe) {
Log.e("serializeObject", "error", ioe);
return null;
}
}
我的插入行
public long insertRow(byte[] data) {
/*
* CHANGE 3:
*/
// TODO: Update data in the row with new fields.
// TODO: Also change the function's arguments to be what you need!
// Create row's data:
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATA, data);
// Insert it into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
然后我插入序列化对象
myDB.insertRow(MyDBAdapter.serializeObject(items));
我也感到困惑。我应该保存适配器或项目列表吗?
我将数据作为BLOB存储在数据库中的方式是将它们转换为JSON,然后存储字节。例如
ArrayList<Person> persons = new ArrayList<>();
Gson gson = new Gson();
ContentValues values = new ContentValues();
values.put(MyProvider.KEY_DATA, gson.toJson(persons).getBytes());
// insert or update the DB
并找回清单
byte[] blob = cursor.getBlob(cursor.getColumnIndex(MyProvider.KEY_DATA));
String json = new String(blob);
Gson gson = new Gson();
ArrayList<Person> persons = gson.fromJson(json, new TypeToken<ArrayList<Person>>()
{}.getType());
编辑:要回答您的最后一个问题,您应该存储数据(项目列表)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句