I have an array of data 10,000 entries long. This data almost never changes and I could handle it in the app if it did anyway. It seems unnecessary for me to store this on an external database considering it is just string data so wont be large.
Is there a way of storing this on the users phone with the app. I was thinking the string resources or a csv file. But these don't seem like the quickest or best option. What are the considerations I need when deciding whether to upload it to a database vs storing it on the device. Currently I use DynamoDB so searching the array on many attributes would be difficult.
Thanks in advance for your help.
If your data will not be change in future . Means its static then put Data in on string.xml
OR
If your data will be change in future then go with it database .....
I would go with storing Strings in a String-Array in your Strings.xmllike so:
<resources>
<string-Array name ="data">
<item> Data 1 </item>
<item> Data 2 </item>
<item> etc etc </item>
</string-array>
</sesources>
and then within your class you would reference the array and store that array into your List
List<String> stringData = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.data)));
for (String row : stringData){
Log.d("Data", row);
}
if you're coding this from a Fragment I suggest using getActivity().getResources().getStringArray(R.array.data) or pass the context to your Fragment.
Seeing the size of your data (10,000), it's not good to use string resources.Also, your data are static which mean they will be access by read only. So it's better to use a file or Sqlite.However, if you do lot of queries to your data such as finding a specific entry, I encourage you to use Sqlite, because you can use SQLite index to query data faster, speed up sort...
Related
I have this kind of data. This can be or don't be an array. Just for easy reference.
ArrayName = Array1, Array2, Array3
Array1 = abc, cde, fgh
Array2 = abc, cde
Array3 = abc, cde, fgh, ijk, lmn
So, what are the best method to store this kind of data.
If I want to
Add or delete Array1 and all things inside
Add or delete item in Array2(eg. adding fgh or remove cde)
Methods I discovered:
SharedPreference Android Shared preferences example
Arrays
SQLite Android SQLite Example
Text file
Please share the pro and cons of why you choose the method.
Please also share if there are better ways to store this kind of data.
Kindly edit this if you found a better link or sample for other to reference.
Here are pros and cons for each solutions:
1) SharedPreference
You save simple key-value pairs here. So it is very hard to save array and complex structures in SharedPreference. So the solution will not work with arrays and arrays of arrays. It will be extremely(but not impossible) difficult to achieve what you want.
2) Arrays
Absolutely not! It is memory storage, so when you close app, or on process death, you will lose all data
3) SqlLite
I would add to this other Databases for android, like Realm.
Good solution. It is structured storage for collection of data. It will be very easy for storing/retrieving data when it is structured as rows. Furthermore you can delete rows easily. You don't have to read whole structure (other arrays) when you need particular row, or particular array (table in this case)
4) TextFile
I don't recommend to store in a text file, but it is possible to do so, you can serialize those arrays to text file, and deserialize. But every time you have to do this, and to read whole structure and parse it even if you want only e.g. Array2. It can be slow when your data becomes bigger.
It's incredibly hard to give advice with such vague requirements, you apparently have data structured as an array of arrays of strings, and you want to store it persistently on Android - and that's basically all we know.
In addition to the solutions mentioned, I would consider using GSON to store this as JSON to disk. While read/write may not have optimal performance, it's very easy to model documents with things like arrays of arrays, and we have no way of knowing your performance requirements vs ease of use.
class MyData {
public List<List<String>> data;
}
If you then have a MyData object, you could simply serialize it to a string, which could be written to a file on disk:
String json = new Gson().toJson(myData);
This would produce something like
{
"data": [
["abc", "cde", "fgh"],
["abc", "cde"],
["abc", "cde", "fgh", "ijk", "lmn"]
]
}
which could easily be written to disk using e.g. standard File and BufferedWriter. You can then read it back and deserialize using:
MyData myData = new Gson().fromJson(json, MyData.class);
Currently, I'm doing an android app, where I stored some categories inside a string array and get them into the spinner.
Now the problem is, if the user needs to add their own category, is it possible to add their categories inside the string array? Or do I need to store these categories inside the database?
This is my string array:
<string-array name="array_categories">
<item>Food</item>
<item>Clothing</item>
<item>Transportation</item>
<item>Entertainment</item>
<item>Halls</item>
<item>Jewellery</item>
<item>Other</item>
</string-array>`
Resources stored in application are generated during compile-time and are read-only during runtime. You can't modify them.
You could try to use SQlite database for storing information. The first time you run application it will take data from XML Array and store in database. Next time when you update from the server you can just add/modify your database.
using Set in sharedpreference is API lv11;
i have a project need to parse to many types of nested items
and need to save it to sharedpreference using only string,
the xml items is very complicated to save as a normal string to
sharedpreference, if i use normal string it need to create
so many sharedpreference names and values,
My question is, is JSON is the alternative because its string is
like a list so that i can read easily the items in per category.
You may want to consider using internal storage over SharedPreferences.
Here is more information on internal storage.
Yes you can use JSON as a string and save it to the SharedPreference that is because JSON is faster , smaller and less verbose structure than XML.
What is the best way to access static data for an Android app
Information about the data
Will be read-only, the user cannot change it
Data will consist of an ID, and a few words/short phrases
It won't be huge: maximum of about 1000 words/short phrases
The data will be accessed sequentially
I will have the data while developing the app
The data will be read frequently
From what I've read on the web so far, I have a few options:
Load all the static data into an SQLite database when the app starts. This is the secure way of doing it because then only rooted users can delete/change data. Also, Android allready has SQLite and querying is easy. Can be read sequentially with cursor.moveToNext() method
Read the data from a flat/xml/csv file from within the app's resources. I don't know how the speed of reading from a file compares to using SQLite? Also, if I've got to reference to a particular line (which will be a word/phrase) in the file, what is the difference between that and just hardcoding it?
What is the best option? Are there better solutions?
You can put your static data in values/strings.xml file.
It is best way if you don't have huge data and user can not change it.
static final data ? :
The common way to do it in android , is to create a file called strings.xml under /res in your application, and to put your strings there. every string has a name. exemple :
<string name="app_name">Single Finder</string>
Then you can access to your strings from your XML layouts or from your java sources. exemple :
XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
Java:
String myStr = getApplicationContext().getRessources().getString(R.string.hello_world);
Another way, which is the Java common way to create static final strings. see Declaring a Variable as a Constant.
I need a sugestion for my app. I need to save in app some arrays of Strings. How can I do this? Which is the best way for doing this?
Thanks in advance...
If the string array is a constant thing, then you can define it in the resources
<string-array name="my_array">
<item>item1</item>
<item>item2</item>
</string-array>
You can grab it from the resources later like this
String[] myArray = getResources().getStringArray(R.array.my_array)
You can use SQLite Database or file in the sdcard to store the strings.It depends on how you need to use them.
If every String in your array is only a 1 or a 0, then you can build one large String of everything and put in a SharedPreference. When you fetch the saved value all you have to do is parse it character by character to get your 1s and 0s again.
There are probably two different solutions:
Using SharedPreferences
Using SQLite Database
You can pick one depending on how many strings you want to save. SharedPreferences is commonly used for storing a small amount of data and making it easy to access it. Databases should be used if you want to store a bigger amount of data.