I am creating an Android application that allows the user to create different lists of friends. For each list, the items have a name and 2 parameters...
我正在创建一个Android应用程序,允许用户创建不同的朋友列表。对于每个列表,项目都有一个名称和2个参数......
EXAMPLE
例
List 1: (Steve, param 1, param 2), (Lisa, param 1, param 2)... etc
清单1:(Steve,param 1,param 2),(Lisa,param 1,param 2)......等
List 2: (John, param 1, param 2), (Steve, param 1, param 2)... etc ...
清单2 :( John,param 1,param 2),(Steve,param 1,param 2)......等等......
What is the best way of dynamically persisting all these lists created by the user?
动态保存用户创建的所有列表的最佳方法是什么?
I was thinking about using SQLite since it is quite easy to implement.
我正在考虑使用SQLite,因为它很容易实现。
I also though about working with plain JSON files, but this approach seemed too resource consuming, because the application deals with adding, reading, modifying stuff...
我也使用普通的JSON文件,但这种方法似乎太耗费资源,因为应用程序处理添加,读取,修改内容......
So, what is the best way of persisting these objects?
那么,持久化这些对象的最佳方法是什么?
If I use a database, I thought it should have the following scheme:
如果我使用数据库,我认为它应该有以下方案:
PERSONS - columns: Person_ID, Person_Name
PERSONS - 列:Person_ID,Person_Name
LISTS - columns: list
列表 - 列:列表
For LISTS, each row is just formed by some sort of set of tuples, representing a single list.
对于LISTS,每一行都是由某种元组集组成,代表一个列表。
EXAMPLE
例
Row 1: [(Person_ID, param1, param2), (Person_ID, param1, param2), ...]
第1行:[(Person_ID,param1,param2),(Person_ID,param1,param2),...]
Where Row 1 is a single list with all the people contained in it.
第1行是单个列表,其中包含所有人员。
Unfortunately this doesn't seem very efficient....
不幸的是,这似乎不是很有效....
I also thought about creating an extra table
我还想过创建一个额外的表
LIST_ROW - columns: row_id, Person_ID, param1, param2
LIST_ROW - 列:row_id,Person_ID,param1,param2
and then each row in the LISTS table would just be a tuple of IDs for all the rows that belong to that list...
然后LISTS表中的每一行都只是属于该列表的所有行的ID元组...
EXAMPLE
例
Row 1 would just become: (row_id, row_id....) representing the elements in the list 1.
第1行将成为:(row_id,row_id ....),表示列表1中的元素。
Unfortunately, I am not sure on how efficient this is.
不幸的是,我不确定这是多么有效。
Can someone tell me what is the best way of achieving this?
有人能告诉我实现这个目标的最佳方式是什么?
2 个解决方案
#1
0
You can use shared preferences for this. Convert your list to a String(comma sepereated objects and values) then add this is shared prefrences. In same way you can get this and convert to your list.
您可以使用共享首选项。将列表转换为String(逗号分隔的对象和值)然后添加这是共享的prefrences。以同样的方式,您可以获得此并转换为您的列表。
#2
0
if the data is no so large,you can save them in sharedpreference, for example: first define the data bean, second add the data bean to arraylist and then save to sp:
如果数据不是那么大,可以将它们保存在sharedpreference中,例如:首先定义数据bean,然后将数据bean添加到arraylist然后保存到sp:
saveObject("list_one", list);
/**
* Save object to SharedPreferences
*
* @param key
* @param obj
*/
protected void saveObject(String key, Object obj) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
String str = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, str);
editor.commit();
} catch (IOException e) {
e.toString();
}
}
/**
* Read object from SharedPreferences
*
* @param key
* @return object
*/
protected Object getObject(String key) {
try {
String str = sp.getString(key, "");
ByteArrayInputStream baim = new ByteArrayInputStream(Base64.decode(str, Base64.DEFAULT));
ObjectInputStream ois = new ObjectInputStream(baim);
return ois.readObject();
} catch (Exception e) {
}
return null;
}
#1
0
You can use shared preferences for this. Convert your list to a String(comma sepereated objects and values) then add this is shared prefrences. In same way you can get this and convert to your list.
您可以使用共享首选项。将列表转换为String(逗号分隔的对象和值)然后添加这是共享的prefrences。以同样的方式,您可以获得此并转换为您的列表。
#2
0
if the data is no so large,you can save them in sharedpreference, for example: first define the data bean, second add the data bean to arraylist and then save to sp:
如果数据不是那么大,可以将它们保存在sharedpreference中,例如:首先定义数据bean,然后将数据bean添加到arraylist然后保存到sp:
saveObject("list_one", list);
/**
* Save object to SharedPreferences
*
* @param key
* @param obj
*/
protected void saveObject(String key, Object obj) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
String str = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, str);
editor.commit();
} catch (IOException e) {
e.toString();
}
}
/**
* Read object from SharedPreferences
*
* @param key
* @return object
*/
protected Object getObject(String key) {
try {
String str = sp.getString(key, "");
ByteArrayInputStream baim = new ByteArrayInputStream(Base64.decode(str, Base64.DEFAULT));
ObjectInputStream ois = new ObjectInputStream(baim);
return ois.readObject();
} catch (Exception e) {
}
return null;
}