-1

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?

CHarris
  • 2,693
  • 8
  • 45
  • 71

2 Answers2

0

Simple way to transfer the data in between two activity you can use bundle

for Example

Gson gsonphoneNumberofContactStringArray = new Gson();    
    String jsonphoneNumberofContactStringArray = gsonphoneNumberofContactStringArray.toJson(phoneNumberofContactStringArray);
    Bundle bundle = new Bundle();
    bundle.putString("phoneNumberofContactStringArray", "Android");
    Intent intent = new Intent(getApplicationContext(), Activity.class);
    intent.putExtras(bundle);
    startActivity(intent);

get data from bundle like this (In your second Activity)

public Bundle getBundle = null;
getBundle = this.getIntent().getExtras();
String json= getBundle.getString("phoneNumberofContactStringArray");

Hope it will help you :)

Raja
  • 2,775
  • 2
  • 19
  • 31
0

As per Omar Aflak's comment above :

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("SelectPhoneContactAdapter phoneNumberofContactStringArray :" + Arrays.toString(phoneNumberofContactStringArray));
CHarris
  • 2,693
  • 8
  • 45
  • 71