Android将对象保存为sqlite中的blob [英] Android Save object as blob in sqlite

查看:613
本文介绍了Android将对象保存为sqlite中的blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是正确的。我有这个 gridview 。我想在sqlite中将它的项目保存为blob。所以当我打开保存sqlite病态数据的列表只需加载保存项目并调用 adapter.notifyDataSetChanged 到我的 gridview



我试过这个,但是得到 NotSerializableException 错误

列表项目= new ArrayList();

  public static byte [] serializeObject(Object o){
ByteArrayOutputStream bos = new ByteArrayOutputStream();

尝试{
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(o);
out.close();

//获取序列化对象的字节
byte [] buf = bos.toByteArray();

return buf;
} catch(IOException ioe){
Log.e(serializeObject,error,ioe);

返回null;
}
}

我的插入行

  public long insertRow(byte [] data){
/ *
* CHANGE 3:
* /
// TODO:用新字段更新行中的数据。
// TODO:同时改变函数的参数为​​你所需要的!
//创建行的数据:
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATA,data);

//将其插入数据库。
返回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());
//插入或更新DB

然后返回列表

  byte [] blob = cursor.getBlob(cursor.getColumnIndex(MyProvider.KEY_DATA)); 
String json = new String(blob);
Gson gson = new Gson();
ArrayList< Person> people = gson.fromJson(json,new TypeToken< ArrayList< Person>>()
{} .getType());

编辑:要回答最后一个问题,您应该存储您的数据(项目列表) p>

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.notifyDataSetChanged to my gridview

I tried this one but I get NotSerializableException error

List items = 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;
        }
    }

my insert row

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);
}

then I insert the serialize object

myDB.insertRow(MyDBAdapter.serializeObject(items));

also i'm confuse. should i save the adapter or the list of items'?

解决方案

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  = new ArrayList<>();
Gson gson = new Gson();
ContentValues values = new ContentValues();
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));
String json = new String(blob);
Gson gson = new Gson();
ArrayList<Person> persons = gson.fromJson(json, new TypeToken<ArrayList<Person>>()
                                 {}.getType());

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

这篇关于Android将对象保存为sqlite中的blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆