简体   繁体   中英

What is the most efficient way of persisting lists?

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...

EXAMPLE

List 1: (Steve, param 1, param 2), (Lisa, param 1, param 2)... etc

List 2: (John, param 1, param 2), (Steve, param 1, param 2)... etc ...

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.

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...

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

LISTS - columns: list

For LISTS, each row is just formed by some sort of set of tuples, representing a single list.

EXAMPLE

Row 1: [(Person_ID, param1, param2), (Person_ID, param1, param2), ...]

Where Row 1 is a single list with all the people contained in it.

Unfortunately this doesn't seem very efficient....

I also thought about creating an extra table

LIST_ROW - columns: 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...

EXAMPLE

Row 1 would just become: (row_id, row_id....) representing the elements in the list 1.

Unfortunately, I am not sure on how efficient this is.

Can someone tell me what is the best way of achieving this?

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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM