0

Am trying to store the values in my ArrayList with SharedPreferences and access them in another class.This is what i have tried so far.

public class sample {

final static String file = "myFile";
SharedPreferences fileName;

protected void onCreate(Bundle savedInstanceState) {

            Sharedpreferences.Editor list = SharedPreferences.edit();
            list.putStringArray("key", 0);
            list.commit();

    }
}

public class sample2 {

String array[] = {"can", "you", "help", "me", "with", "this?"};

protected void onCreate(Bundle savedInstanceState) {

         Random random = new Random();
         int x;
         x = random.nextInt(5);

         String generatedValue = arr[x];

         String get = fileName.getString("key", 0);
         SharedPreference.Editor editList = SharedPreferences.edit();
         editList.putString("key", get);
         editList.commit();
         }
}

Any help would be appreciated.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
terch
  • 41
  • 4
  • You will get more help asking questions regarding a `problem`.That said,what exactly are you trying to do? – Ojonugwa Jude Ochalifu Feb 16 '14 at 16:56
  • i want to store the values of my string array to the arraylist from the other class using sharedpreferences – terch Feb 16 '14 at 17:04
  • Activities under the same Application package can read the same SharedPreferences file. – DeeV Feb 16 '14 at 17:09
  • my problem is, I want to store those values in the arraylist permanently, and generate another values not equal to the existing values of the arraylist and store – terch Feb 16 '14 at 17:11
  • Read [this](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) it should tell you everything you need to know about using SharedPreferences. – Ojonugwa Jude Ochalifu Feb 16 '14 at 17:13
  • [This](http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) is also a perfect example of how to save arraylists in sharedpreferences.Good luck – Ojonugwa Jude Ochalifu Feb 16 '14 at 17:36
  • `list.putStringArray` ? – Mr_and_Mrs_D Feb 17 '14 at 01:50

0 Answers0