2017-09-23 6 views
-1

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?

+0

voulez-vous transmettre un tableau à une autre activité? –

+0

Oui, c'est exactement ce que j'essaie de faire. – CHarris

+0

vérifier mon ci-dessous et https://stackoverflow.com/a/46379769/7666442 –

Répondre

0

Une façon simple pour transférer les données entre deux activités, vous pouvez utiliser bundle

pour l'exemple

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); 

obtenir des données de paquet comme celui-ci (dans votre deuxième activité)

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

J'espère que ça va vous aider :)

0

Selon Omar Aflak's comme nt ci-dessus:

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));