-1

I'm currently building an app which will have pre-saved data for in-app use, but I'm not sure how should I store my data in the app, and would love some help and ideas on that:

These are the main "specs" and important information about my data:

  1. The data is defined by me (the developer) and is not affected in any way by the user (This means the data is static - no need to update it at anytime except when there is a new update to the app).
  2. The data will be divided into distinct items. There will be about 50-200 items in the app.
  3. Each item will have info (i.e name, type, etc..), including resources for map vectors and images.
  4. The items will be loaded into a ListView
  5. There will be option to filter the items by one specific property (in the ListView).
  6. Since all the data is generated by me, and some of it is big, I would like if possible for an easy way of "writing" the data.

I was thinking of two options:

  1. Define a Java Class for the items, and populate a static class with all the items, stored in a list.
  2. Use a SQLite DB, but that seems too "overpowered" since the data is static.

What other options are available and what are the tradeoffs between them?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Mickey
  • 1,405
  • 2
  • 13
  • 33
  • Do you need to store the data permanently? A Java class alone will not do so, even if it is a `static` variable. As soon as the user closes your app, the variable will be destroyed. – Code-Apprentice Sep 07 '17 at 15:14
  • @LunarWatcher, I don't believe this is opinion based since there are more and less efficient way of storing the data. – Mickey Sep 07 '17 at 15:15
  • @Code-Apprentice What do you mean in permanently? The data will be "hard-coded" to the app, and will need to available for all of the app life-span. – Mickey Sep 07 '17 at 15:15
  • Hard-coding strings in code is about as permanent as it gets. On my first scan of your question, I missed that the data is completely static. – Code-Apprentice Sep 07 '17 at 15:16
  • @LunarWatcher I understand why you think this is opinion based but I'll explain why I think this is not fully opinion based. There are "bad ways" of storing the data, which will be too overpowered or inefficient. Since I'm a beginner android developer this is my first encounter with need to store hard-coded data, and I'm sure there are some options available which are better and some are worse, for them I wanted some insight. – Mickey Sep 07 '17 at 15:21
  • Bad ways of storing data **are opinion based**. Some would claim it is better to use static final strings, other would claim files in assets or resources is better, you get the idea. And there are about to be 5 close votes on this question which closes it as POB. So yeah, this question is opinion based. And there it is closed. – Zoe Sep 07 '17 at 15:22
  • @LunarWatcher I choose to read the question as "What are my options?" rather than "What is the best approach?" With this interpretation, the question is not opinion-based, but does appear to be a duplicate which has a link above. – Code-Apprentice Sep 07 '17 at 15:28
  • @Code-Apprentice gold-medal [tag:android] user closed the question as a duplicate. Still 4 votes to POB – Zoe Sep 07 '17 at 15:29
  • @LunarWatcher In other words, not everyone agrees that this is primarily opinion-based. – Code-Apprentice Sep 07 '17 at 16:31

3 Answers3

0

For text data, you have several options:

  1. XML string and string array resources.
  2. A SQLite database.
  3. A flat file.
  4. final static String variables in Java code.
  5. SharedPreferences.

Option 4 is typically used for settings, not large amounts of data. The other options all have their trade offs and seem like reasonable solutions.

For digital resources, like images, you have at least two options:

  1. Distribute everything with your app under the res or raw folders in your Android Studio project.

  2. Provide the resources as downloads.

For large resources, the second method is preferred since packaging them with your app will make the initial download very large. If you can provide resources as a separate download, they can be downloaded as needed rather than all at once during installation.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thanks for the detailed answer! I will also look for the answer which was marked as duplicated. Seems like either a flat file can be the appropriate solution. Maybe a csv file with the class properties, which I will load to that class list with a buffered reader? Is that a viable solution? thanks again – Mickey Sep 07 '17 at 15:30
  • @Mickey CSV qualifies as a flat file. Without more details about the nature of the data, I cannot comment on how well this solution suits your particular situation. – Code-Apprentice Sep 07 '17 at 16:32
-1

If you want to store text data you should use string-array in resources

Andrew
  • 45
  • 1
  • 8
  • May as well use `string` resources. And this is barely an answer – Zoe Sep 07 '17 at 15:18
  • An author wrote "There will be about 50-200 items". It's much more comfortable to work with such data in case of string-array and data will be static while using String objects will make it non static. – Andrew Sep 07 '17 at 15:23
  • You clearly don't know the Android framework. Ever heard of [string resources](https://developer.android.com/guide/topics/resources/string-resource.html)? Same as array resources (static) except the arrays are arrays and not single Strings. String objects in Java are final if they are declared as `final` otherwise, they are not static – Zoe Sep 07 '17 at 15:24
  • Using string-array you are able to define sub-items like "name" or "type". So each data item will be a string-array and it will contain several sub-items with info. – Andrew Sep 07 '17 at 15:32
  • YOu can use either. If you want it to be categorized and labeled, go with arrays. Otherwise strings – Zoe Sep 07 '17 at 15:33
-1

Use SQLite. It's not going to significantly slower the efficiency of your application(and that is something you really shouldn't worry about). SQLite is already light enough - especially if you're only using 200 items.

Look at https://github.com/jgilfelt/android-sqlite-asset-helper for pre-loaded databases and create a singleton db handler class. You can create a listview or recyclerview using this to populate data.

AeriaGlorisia
  • 229
  • 3
  • 9