在Android的SQLite数据库保存的ArrayList [英] Saving ArrayList in SQLite database in Android

查看:1169
本文介绍了在Android的SQLite数据库保存的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用的SQLite在Android上,我想一个ArrayList添加到表中的一列,然后获取数据传回作为一个ArrayList。该ArrayList是多头的列表。我注意到,SQL有用于存储BLOB的选择,但它看起来像我需要首先能够将其存储在我的SQLite数据库中的BLOB前的数组列表转换为一个byte []。

I've been working with SQLite on android and I would like to add an arraylist to a column in a table, and then fetch the data back as an arraylist. The arraylist is a list of Longs. I've noticed that SQL has an option for storing BLOBS, however it looks like I need to convert the arraylist to a byte[] first before being able to store it as a blob in my SQLite database.

如果任何人有如何保存的ArrayList到一个SQLite数据库,这将是极大的AP preciated的解决方案。或者有没有其他的选项,救了我的数据阵列,我应该考虑?

If anyone has a solution on how to save arraylists into an SQLite database that would be greatly appreciated. Or is there any other option for saving my array of data, i should consider?

推荐答案

要插入:

ArrayList<String> inputArray=new ArrayList<String>();

// ...添加值inputArray

//....Add Values to inputArray

Gson gson = new Gson();

String inputString= gson.toJson(inputArray);

System.out.println("inputString= " + inputString);

使用inputString保存ArrayList的价值在SQLite数据库

use "inputString" to save the value of ArrayList in SQLite Database

中检索:

获取从SQLiteDatabse字符串保存并转换成ArrayList的类型如以下: outputarray是一个字符串,它是SQLiteDatabase得到的这个例子。

Get the String from the SQLiteDatabse what you saved and changed into ArrayList type like below: outputarray is a String which is get from SQLiteDatabase for this example.

Type type = new TypeToken<ArrayList<String>>() {}.getType();

ArrayList<String> finalOutputString = gson.fromJson(outputarray, type);

  • 在SQLite的使用文本格式存储的字符串值.....
  • 这篇关于在Android的SQLite数据库保存的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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