I have an Array with integer values. It will grow over time. It will have approximately up to 50 values.
I want to store the array persistent and thus I thought about storing it in SharedPreferences.
I know that no complex types can be stored in it, but I also heard about to serialise the Array and then store it in SharedPreferences.
Can someone give me a hint or even better sample code how to do that?
Not very efficient way, but will get the job done:
SharedPreferences prefs = ...;
final int count = 50;
final String KEY_COUNT = "COUNT";
final String KEY_VAL_PREFIX = "VAL_";
int values[] = new int[count];
/*
* ... put some stuff in values[] ...
*/
final Editor sped = prefs.edit();
sped.putInt(KEY_COUNT, count);
for (int i = 0; i < count; i++)
{
sped.putInt(KEY_VAL_PREFIX + i, values[i]);
}
sped.commit();
Then later you can retrieve these values by grabbing the KEY_COUNT value from the prefs, then filling your empty array with values2[i] = getInt(KEY_VAL_PREFIX + i, 0) calls.
You may use ObjectSerializer to do it. Here is SO discussion on how to do.Store Shared preferences
I would convert your array to a string of values separated by commas. And then store the string as a single key-value pair.
Then, when you want to extract the array, simple use the split function to split the string up into array elements based on a comma separator.
Related
I am trying to learn retrofit and I have made successful attempts at posting data and now I am trying to retrieve JSON array which looks as follows:
{
"result": "success",
"message": "All Questions Have Been Selected",
"question": {
"all_question_ids": ["1","2","3"]
}
}
I am using the following getter
public ArrayList getAll_question_ids(){
return all_question_ids;
}
I am retrieving using Retrofit as follows
if (resp.getResult().equals(Constants.SUCCESS)) {
SharedPreferences.Editor editor = pref.edit();
Log.d("Question_IDs", "getAllQuestionID() = " + response.body().getQuestion().getAll_question_ids() );
editor.putString(Constants.All_QUESTION_IDS,((resp.getQuestion().getAll_question_ids().toString())));
editor.apply();
}
progress.setVisibility(View.INVISIBLE);
It is here that I am stuck, as I am retrieving the array ok but I am unsure how to loop out the Array which is now stored in Shared Preferences.
When I place a toast to show me how the IDs are coming across, my toast confirms the data as [1,2,3]
The goal is to add a dynamic button and the individual ID, i.e button 1, button 2 etc every-time the loop is iterated.
I have tried the following:
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
for (int i =0; i < questionNumber.length(); i++) {
try {
/*Dynamically create new Button which includes the question name
*/
AppCompatButton btn_question = new AppCompatButton(getActivity());
/*LayoutParams (int width, int height,float weight)
As LayoutParams is defaulted in px, I have called a method called dpToPX to make sure
the dynamically added EditText is the same size on all devices.
*/
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dpToPx(280), dpToPx(45), 1);
btn_question.setBackgroundColor(Color.parseColor("#3B5998"));
btn_question.setTextColor(Color.WHITE);
// btn_question.setText(String.valueOf(x));
btn_question.setText("Question "+ pref.getString(Constants.All_QUESTION_IDS,""));
btn_question.setGravity(Gravity.CENTER);
//generate unique ID for each new EditText dynamically created
View.generateViewId();
//Log.d("TEST VALUE", "Question1 generated ID = " + btn_question.generateViewId());
params.setMargins(0, dpToPx(10), 0, dpToPx(10));
btn_question.setPadding(0, 0, 0, 0);
btn_question.setLayoutParams(params);
allEds.add(btn_question);
mLayout.addView(btn_question);
} catch (Exception e) {
Log.d(TAG, "Failed to create new edit text");
}
}
However the above is adding the value as it appears in the array e.g [1,2,3] which is obviously not what I want.
I have added a photo in case my explanation isn't clear. I want a button with 1 number added to it each time the loop iterates but I am unable to figure this out.
I have looked through lots of resource but cannot find an answer that is relevant to my problem, although, if there is, I am not familiar enough to recognise a similar issue.
If someone can offer some assistance, I would appreciate it!
When you call editor.putString(Constants.All_QUESTION_IDS,((SOMETHING.toString())));, what is actually stored depends on the implementation of the toString method in the type of SOMETHING (in this case String[]). So avoid doing that. Instead, since you're already using Gson or Jackson (or others), store the question_idsas JSON:
final String jsonIds = gson.toJson (resp.getQuestion().getAll_question_ids());
editor.putString(Constants.All_QUESTION_IDS, jsonIds);
Your actual stored value no longer depends on the implementation of something that you don't control (String[].toString). It is a valid JSON array and regardless of what tool/library you use to read it back, it's valid.
Now, to read back the stored data:
final String storedJson = pref.getString(Constants.All_QUESTION_IDS, null);
if (null == storedJson) {
// TODO: No question ids found
}
final String[] ids = gson.fromJson (storedJson, String[].class);
for (int i = 0; i < ids.length; i++) {
// make your buttons
}
This is a problem of saving and then reading out a List of items (in this case, String instances).
You've chosen to save the list by calling editor.putString() with a value of getAll_question_ids().toString(). That toString() call is going to return a string representation of your list, or, in other words, a String instance with the value [1, 2, 3]. At this point, you no longer have a List proper, but a String that looks like a list.
This is all technically fine, but it means you have to take this into account when you're trying to read out that list.
You've written this to read the list back out:
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
Once this line executes, questionNumber will be a String instance with the value [1, 2, 3]. Again, this is fine, but now we come to the key point: we have to convert this String back into a List.
If you know for sure that the values in this list won't have commas in them, you can do it easily:
Trim the braces off the string using substring()
Create a String[] using split()
Convert your array to a list using Arrays.asList() (you could even skip this step since iterating over an array is just as easy as iterating over a list)
Put that together and you get:
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
questionNumber = questionNumber.substring(1, questionNumber.length() - 1);
String[] array = questionNumber.split(", ");
List list = Arrays.asList(array);
At this point, you can iterate over your array or list:
for (String value : list) {
...
btn_question.setText("Question " + value);
...
}
Please be patient while I explain my issue:
1) I am storing my preferences via a StringSet as follows:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
// Create a new Arraylist with the details of our details
ArrayList <String> newCityFareDetails = new ArrayList<String>();
// Store various values
newCityFareDetails.add(0, String.valueOf(cloneFare.value1()));
newCityFareDetails.add(1, String.valueOf(cloneFare.value2()));
newCityFareDetails.add(2, String.valueOf(cloneFare.value3()));
newCityFareDetails.add(3, String.valueOf(cloneFare.value4()));
newCityFareDetails.add(4, cloneFare.value5());
// Only value 5 is a string, rest are all floats
// Convert to a hashstring, give it the name of our value
Set<String> set = new HashSet<String>();
set.addAll(newCityFareDetails);
editor.putStringSet(extras.getString("startCity"), set);
// And write it to storage
editor.commit();
Now, I'm trying to read it as follows:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
Set<String> tryCityFromPrefs = prefs.getStringSet(currentCity, null);
if (tryCityFromPrefs!=null){
// Crude code, but we convert the preferences into a String array
String[] values = tryCityFromPrefs.toArray(new String[tryCityFromPrefs.size()]);
myFare = new Fare(Float.parseFloat(values[0]), Float.parseFloat(values[1]),
Float.parseFloat(values[2]), Float.parseFloat(values[3]), values[4]);
}
Now, problem is that the myFare is not getting initialized properly because the values in the array are scrambled. i.e. the String value that was at the last position when we save is now in the 2nd position. Is this something to do with Sets to String conversion? Or am I missing something obvious?
A Set does not guarantee order. While there are specific Set implementations (e.g., LinkedHashSet) that are ordered, that's not what SharedPreferences uses.
Your options are:
Change your app to not care about the order.
Save the data in SharedPreferences some other way. In this app, for example, I use JsonReader/JsonWriter to save an ArrayList into a single String value.
Save the data in some other fashion (e.g., JSON file, SQLite database with a sequence number to maintain order).
I have an array in a shared preferences, and an array with values in my class in Android.
The thing is, I will generate a number that is equal to the index of my array in my class
and store that generated values to the array in my shared preferences without duplicate,
For example:
String array[] = {"can", "you", "help", "me"}
Random ran = new Random();
int x = ran.nextInt(4);
String generatedValue = array[x];
The generatedValue will be stored to array in my shared preference.
I have tried creating 120 keys cause I have 120 indexes in my array but it's not the proper way of storing it to the shared preferences.
Here is what I have done:
SharedPreferences.Editor edit1 = firstFile.edit();
edit1.putString("key1", "");
edit1.commit();
and so on and so forth
im using MultiSelectListPreference and the values save on array..
How can read??
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
Set<String> a = pref.getStringSet("tabs", null);
for ( int i = 0; i < a.size(); i++) {
Log.d("salida", a[i]);
}
i get this error: The type of the expression must be an array type but it resolved to Set
You want to use the Set, and since it isn't an array, the square brackets ([])are cannot be used to access indexes.
To easily read the values from the Set,use the enhanced for loop:
for (String str: a){
Log.d("salida", str);
}
If you want to remove items from that Set as you loop through, you will have to use an Iterator, as shown in this answer.
Alternatively, if you want an array, you can use Set#toArray():
String [] prefStrings = a.toArray(new String[a.size()]);
Then you can use the square brackets (prefStrings[position]) to access an index.
I'm currently working on an Android application that is going to be somewhat like a student planner. My backend is all java and I am currently stuck because i am storing the objects that i create from my backend to arraylist. Being java, these objects dissapear as soon as the programs terminate. Whats the easiest way that i can store my java objects for retrieval the next time i boot my application? Any help is much appreciated! Im developing on 2.3 and eclipse(juno).
One of the data storage options listed in the Android developer tutorial will be the easiest thing to do. Which is the best fit will depend on how much data you're storing and how you need to access it. As the page says, the SharedPreferences class is best for a handful of items; for larger data sets, you could use Java serialization or some other way to write them to a file on the phone's storage; and if your data are large and/or you need structured access to them, an SQLite database is your best bet.
You can use Shared Preferences to store your data. The SharedPreferences lets you persist key-value pairs of primitive data types in you application. You can't store an entire ArrayList with a single key, but you can iterate over the array and systematically generate a key for each value in the list. I usually do something like this:
public class SomeActivity extends Activity {
private ArrayList<Data> list; //We'll persist this array
/* snip */
//These strings can be anything - you just need something you can use to systematically generate distinct keys
public static final String LIST_KEY = "SomeActivity_List";
public static final String LIST_LENGTH_KEY = "SomeActivity_ListLength";
/**
* How this method works: It starts by getting a SharedPreferences object,
* which offers an API for persisting data. It then systematically generates
* Strings like "SomeActivity_List1", "SomeActivity_List2", "SomeActivity_List3",
* and so on to use as keys fot the 1st, 2nd, 3rd, etc. elements in the list. Then
* it Data.saveData(), a method defined below in the Data class, in order to give
* each Data object in the ArrayList an opportunity to persist its primitive
* members in the SharedPreferences.
*
* SomeActivity.restoreList() works similarly.
*/
public void saveList() {
SharedPreferences prefs = getPreferences(); //This method is part of the Activity class
SharedPreferences.Editor editor = prefs.getEditor();
//Save the length of the list so that when we restore it, we know how many
//Data objects to recreate.
editor.putInt(LIST_LENGTH_KEY, list.size());
editor.commit();
//This for loop is important - note how we concatenate i to each of the keys to give each element in list a distinct key
for(int i = 0; i < list.size(); i++)
{
String identifier = LIST_KEY + Integer.toString(i); //generate distinct keys
list.get(i).saveData(identifier, prefs);
}
}
public void restoreList() {
SharedPreferences prefs = getPreferences();
int length = prefs.getInt(LIST_LENGTH_KEY);
for(int i = 0; i < length; i++)
{
String identifier = LIST_KEY + Integer.toString(i); //re-generate distinct keys
Data data = new Data();
data.restoreData(identifier, prefs);
list.addLast(data);
}
}
public static class Data {
private int i;
private double j;
private String s;
public static final String I_KEY = "Data_I"
public static final String J_KEY = "Data_J" //strings can really be whatever, as long as they're distinct.
public static final String S_KEY = "Data_K"
/**
* How this method works: The SomeActivity.saveList() method generates a
* unique String ("identifier") for each of the Data objects it contains.
* This method uses that distinct string and makes some more distinct keys
* to store each of Data's primitive members.
*
* restoreData() works similarly when it rebuilds Data objects
*/
public saveData(String identifier, SharedPreferences prefs) {
SharedPreferences.Editor editor = prefs.getEditor();
editor.putInt(I_KEY + identifier, i);
editor.putDouble(J_KEY + identifier, j);
editor.putString(S_KEY + identifier, s);
editor.commit();
}
public restoreData(String identifier, SharedPreferences prefs) {
i = prefs.getInt(I_KEY + identifier);
j = prefs.getDouble(J_KEY + identifier);
s = prefs.getString(S_KEY + identifier);
}
}
}
This approach work recursively. If Data had an ArrayList as one of its fields, for example, it could systematically store all of the values in that list in SharedPreferences as well.
FYI: One of the implications of using SharedPreferences is that if the user uninstalls your app or clears the app data, the stored list will be deleted. Depending on the nature of your data, you may or may not want this behavior.