J'utilise Gson
pour transférer un string array
à une autre activité avec SharedPreferences
(parce que SharedPreferences
fonctionne comme est le meilleur avec des chaînes seulement, apparemment)pourquoi ce code ne passe pas mon tableau de chaînes à une autre activité avec Gson?
Pouvez-vous me dire ce qui ne va pas avec ce code:
En activité 1 (c'est un adaptateur personnalisé mais je ne pense pas que ce soit un problème) j'écris la valeur de préférence partagée comme ceci:
//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
montre The value is : [Ljava.lang.String;@424b1370
Pourquoi n'imprime-t-il pas mon ensemble de chaînes?
Et Activité 2 J'essaie de récupérer la valeur SharedPreferences
avec:
//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
montre The value is : [Ljava.lang.String;@424b1370
Pourquoi ne pas imprimer mon tableau de chaînes?
voulez-vous transmettre un tableau à une autre activité? –
Oui, c'est exactement ce que j'essaie de faire. – CHarris
vérifier mon ci-dessous et https://stackoverflow.com/a/46379769/7666442 –