Android将对象另存为sqlite中的blob

用户名

这是正确的吗。我有这个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));

我也感到困惑。我应该保存适配器或项目列表吗?

克里斯·马戈尼斯(Chris Margonis)

我将数据作为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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将C#对象另存为Oracle中的Blob

将对象引用另存为Java中的十六进制数字?

Cosmos DB:将对象类型另存为文档中的属性

Uctc日期在Sqlite中另存为本地日期

将pdf文件另存为blob

我可以在Heroku中将对象另存为配置变量吗?

无法将图像另存为SQLite

尝试从网址中抓取网络数据使用框架。在MSHTML.HTMLDocument对象中获取数据。我想将对象另存为.xls到硬盘上

将Blob对象另存为服务器上的文件

将文件另存为数据库Ajax PHP PDO中的Blob

将文件另存为MYSQL数据库中的Blob或文件路径

逻辑应用将CSV文件另存为Blob存储中的字节

从werkzeug.datastructures.FileStorage计算md5,而无需将对象另存为文件

边缘另存为

如何从图像中删除轮廓最大的对象并将其另存为单独的图像?

将我们自己的对象另存为TreeMap中的键

使用Python将csv文件中的剥离文本另存为字符串对象

将字节数组另存为jpg文件在android设备文件夹中

Android Studio文件菜单中是否有“另存为”选项?

如何将整个画布另存为android画布中的jpeg图像?

将Java对象图另存为XML文件

tabulator - 如何仅提取数据并另存为 json 对象

使用猫鼬将嵌入文档另存为对象?

无法将日期对象另存为字符串

将$ http响应对象另存为$ scope变量

XlsxWriter / OpenPyXl对象另存为HttpResponse以创建下载

将Google API请求另存为Javascript对象

尝试将XLSM另存为CSV时出现“对象_workbook的方法另存为失败”错误

UserSettings中的FileInfo是否另存为Null?