Saving arrays of Strings in Android? - android

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.

Related

Android: Storing an array with the app

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

What is the alternative for storing set in sharedPreference using only api 8 in android?

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.

Adding elements and saving that elements to array that is created in strings.xml

Suppose Type is the name of the array which contains A,B,C,D as elements and is created in strings.xml.Now in the form user can either select any of the elements or add new elements.Suppose user adds E,F,G .Now what i want to achieve is that anyhow the Type array have A,B,C,D,E,F,G in it. Using sqlite is done.But i want to save it in the Type array only and not anywhere else.Is it possible ?
Since anything that you define in res folder acquires a unique id in R.java, which is an array, and size of array cannot be changed at runtime.. So, it is not possible.
You can only save data to persistent storage like SQLite data base, shared preferences or a text file, each of which have different strengths and weaknesses and are suited for different situations, so take your pick.

Maximum Size of SharedPreferences class

I want to save a lot of strings with SharedPreferences class .
These strings are quit long.
I really want to know the maximum length of a string that can be save in shared preferences in android.And Also How much size of data i can store in This SharedPrefernces class.
As per android architecture there is no such limit to store data in SharedPreference. Better way is to database (SQLite) when you have to deal with huge amount of data
I read somewhere that there is no hard limit other than Integer.MAX_VALUE ( maximal string length). But it is not advisable to store that much on shared preferences, as this is XML file which must be parsed and you will have a problems while parsing it.
I used to store about 50-100KBytes there. It worked.
The exact answer obtained manually is: maximum Unicode symbol's size is 5657632 symbols (or from [0 to 5657631]) in my case. It's some about 2.7MB for SharedPReference.Editor .
Rather large storage.
You ca use this size twice:
PreferenceManager.getDefaultSharedPreferences(c)
context.getSharedPreferences("<key>", <Mode>);
Of course is't limit for SharedPreferences but if system won't have enough memory it is one first stuff what DELETE it, you remember it.

Android: Can I save more than one SharedPreference entry to a file?

This is the problem:
I have a form for inputting names, then saves it, then displays it in another activity. But once I try to enter another name, the previous name is overwritten by the new name.
Is there a way to set these names up to list themselves one after another without overwriting each other in SharedPreferences?
You can as long as they have distinct names. Ig you need multiple values for same name, you can store JSON array or use some form of prefix / suffix solution to provide unique names
Either do it like Konstantin Pribluda suggested, or you might think of using the SQLite, if you have a lot of names you want to store (e.g. if creating a history of typed in names). That way you can store unlimited values for the same key and retrieve them as a list/cursor.
But of course that's overkill if you only have 2-3 names…
you can also save a Set of Strings in the SharedPreferences.

Categories

Resources