-2

I have been trying to figure out the best method of saving a string array after an app closes and retrieving the same string information when it is opened again. The context is that I've figured out how to parse data from a website and store it in the string array, however I want to be able to save the string array in the case that the user does not have internet access the string array can be retrieved.

This is being done in android studios in java - just making that clear

I have been looking into sharedpreferenences methods but haven't a working solution. If anyone can help me I would be greatly appreciated!

Lets say the string array is:

String[] webValues = new String[(this value can be very large)];

I am looking to store the entire string array and retrieve the entire string array and then to be able to call the array by specific indices within the activity.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Modest
  • 53
  • 1
  • 7
  • `the best method of saving a string array after an app closes`. Impossible. You should save before the app closes. – greenapps Nov 20 '17 at 20:49

2 Answers2

0

I don't think SharedPreferences allow you to write an array, so you must either write each value individually using the putString function or in API level 11 or later you can use the putStringSet function to save a Set of strings but this will remove any duplicates if you convert an Array to a Set. What I would suggest for your situation is storing each string individually using the index as the key, so something like so should work for you for saving the data

public boolean saveArray(String[] array, String arrayName, Context mContext) {   
  SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
  SharedPreferences.Editor editor = prefs.edit();  
  editor.putInt(arrayName +"_size", array.length);  
  for(int i=0;i<array.length;i++)  
    editor.putString(arrayName + "_" + i, array[i]);  
  return editor.commit();  
} 

and to load the array

public String[] loadArray(String arrayName, Context mContext) {  
    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
    int size = prefs.getInt(arrayName + "_size", 0);  
    String array[] = new String[size];  
    for(int i=0;i<size;i++)  
        array[i] = prefs.getString(arrayName + "_" + i, null);  
    return array;  
}  

taken from this post Is it possible to add an array or object to SharedPreferences on Android by https://stackoverflow.com/users/833622/sherif-elkhatib

James Russo
  • 578
  • 3
  • 18
0

Use ArrayList instead of String array, is easier.

Then transform your arraylist into Set and use this :

   SharedPreferences.Editor editor = shared.edit();
   Set<String> set = new HashSet<String>();
   set.addAll(yourArrayList);
   editor.putStringSet("values", set);
   editor.apply();

To get the values:

 List<String> yourValues = new ArrayList();
    Set<String> set = shared.getStringSet("values", null);
    yourValues.addAll(set);
diegoveloper
  • 93,875
  • 20
  • 236
  • 194