I'm using Gson
to transfer a string array
to another activity with SharedPreferences
(because SharedPreferences
as is works best with strings only, apparently)
Can you tell me what is wrong with this code ?:
In activity 1 (it's a Custom Adapter but I don't think this is an issue) I write the Shared Preference value like this:
//It looks like Shared Preferences only works easily with strings so best way to bring the
// string array in Shared Preferences is with Gson.
SharedPreferences sharedPrefsphoneNumberofContactStringArray = PreferenceManager.getDefaultSharedPreferences(_c);
SharedPreferences.Editor editorphoneNumberofContactStringArray = sharedPrefsphoneNumberofContactStringArray.edit();
Gson gsonphoneNumberofContactStringArray = new Gson();
String jsonphoneNumberofContactStringArray = gsonphoneNumberofContactStringArray.toJson(phoneNumberofContactStringArray);
editorphoneNumberofContactStringArray.putString("phoneNumberofContactStringArray", jsonphoneNumberofContactStringArray);
editorphoneNumberofContactStringArray.commit();
System.out.println("The value is :" + phoneNumberofContactStringArray);
System.out.println
shows The value is : [Ljava.lang.String;@424b1370
Why does it not print my string array?
And in Activity 2 I try to fetch the SharedPreferences
value with:
//we are fetching the string array phoneNumberofContactStringArray, created in Custom Adapter.
//with this we will put all phone numbers of contacts on user's phone into our ListView in NewContact activity
SharedPreferences sharedPrefsphoneNumberofContactStringArray = PreferenceManager.getDefaultSharedPreferences(getApplication());
Gson gson = new Gson();
String json = sharedPrefsphoneNumberofContactStringArray.getString("phoneNumberofContactStringArray", "");
Type type = new TypeToken<String[]>() {
}.getType();
phoneNumberofContactStringArray = gson.fromJson(json, type);
System.out.println("The value is :" + phoneNumberofContactStringArray);
System.out.println
shows The value is : [Ljava.lang.String;@424b1370
Why does it not print my string array?