1

I'm making an Android application and want to create a "Favorites" list for some objects in the app. I wanna make the list accessible and editable in all my activities and I can't really figure out the best way to do this.

Shared preferences? Writing a small txt file to the device? What's the fastest way to do this?

Thanks in advance!

Daniel N.
  • 357
  • 2
  • 20
  • if you want to have the list forever, must insert them to sqlite database. but if your data is not important, use shared preferences. if you have cleaner apps, when cleaning your app, shared preferences will be removed. – Hossein Badrnezhad Jul 15 '18 at 03:24

2 Answers2

0
dependencies {
    compile 'com.google.code.gson:gson:2.3.1'
}

Then when you want to save, convert your array into String:

    ArrayList<Type> yourData = new ArrayList<Type>();
    String dataStr = new Gson().toJson(yourData);

//put this dataStr in your shared preferences as String

    To retrieve and convert back to an object is also simple:

    String str = "";

//you need to retrieve this string from shared preferences.

    Type type = new TypeToken<ArrayList<Type>>() { }.getType();
    ArrayList<Type> restoreData = new Gson().fromJson(str, type);
Rohit Sharma
  • 1,384
  • 12
  • 19
0

If you want to create a Favorites list, use a SQLite Database.

There's really only four ways to store data.

  1. Shared Preferences

  2. Databases

  3. Local files

  4. Remote Server - Slowest since it depends on network connection, so let's skip this.

Between the remaining 3, SharedPreferences is a great option when used to store a single value. However, it's not a good option for storing a Favorites list, mainly because you can only store a single String.

You can still store it by combining all items in your list into one string, then splitting it each time. However, as your Favorites list gets larger, this single long String will too. Splitting and combining all the time isn't efficient.

SharedPreferences is still a decent option if you only have to store the Favorite's list, but since you want to edit it too, it becomes a less attractive solution.

Local Files and Databases are the better options, however local files require you to read in the file each time you want to use it. This process of reading and writing to a file isn't as efficient as using a Database, especially if you want to edit. For example, let's say you want to remove an item from the middle of your Favorite's list. This would require you to read in the file, edit it, then write the change into the file again. Not too pleasant when compared with the ease of the final solution.

Databases are the best option for this, mainly because it's designed to manage data. You can create a single Favorite's table and add each item as it's own individual row. Fetching the entire table becomes quick and easy. Fetching a single item becomes quick and easy. Adding a new item or removing a new item is also quick and easy.

Jackey
  • 3,184
  • 1
  • 11
  • 12