-1

how can I save a shared preference to an array, I tried a couple of things but I just can't do it, I want to save a specific key into the array and then put them in a textview and it just crash. please help guys.

public String[] fetchAllPreference(){
    SharedPreferences sharedPref = getSharedPreferences("DictionaryInfo", 
    Context.MODE_PRIVATE);
    //here i want the code of making it into array come
    return values;
}
public void loadPicture()
{
    gallery.removeAllViews();
    String[] array =fetchAllPreference();
    for(int i=0;i<array.length;i++)
    {
        TextView iv = new TextView(this);
        LinearLayout.LayoutParams layoutParams=new 
        LinearLayout.LayoutParams(100,100);
        layoutParams.setMargins(10,10,0,0);
        iv.setLayoutParams(layoutParams);
        iv.setText(array[i].toString());
        gallery.addView(iv);
    }
}
doric2000
  • 5
  • 3

2 Answers2

0

I know it isnt the exact answer, but it is possible to save each element of the array with the index being the keyword or index assossiated with some string being the keyword.

Harsh
  • 363
  • 4
  • 14
  • yeah i know , already saved , now i want to put those in array. – doric2000 Nov 02 '17 at 15:48
  • have u tried to put the elements to the array using a loop , when the key is index , and copying them to array[index] using getString(String key, String defValue) – Harsh Nov 02 '17 at 16:27
0

i think what u want is to save values in sharedpreference to an array, in that case

SharedPreferences prefs = getSharedPreferences("myFavs", 0);
    Map<String, String> m = (Map<String, String>) prefs.getAll();

a map can be coverted to a collection (https://developer.android.com/reference/java/util/Map.html) and collection can be converted to an array (https://developer.android.com/reference/java/util/Collection.html)

pls read this, it seems similar

Getting shared preferences and displaying them in a listview

Harsh
  • 363
  • 4
  • 14