为什么这段代码不能将我的字符串数组传递给Gson的另一个活动?

问题描述:

我使用Gsonstring array转移到另一个活动与SharedPreferences(因为SharedPreferences为仅与字符串的效果最好,显然)为什么这段代码不能将我的字符串数组传递给Gson的另一个活动?

你能告诉我什么是错的验证码?

在活动1(这是一个自定义适配器,但我不认为这是一个问题),我写的共享偏好值是这样的:

 //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显示The value is : [Ljava.lang.String;@424b1370

为什么不打印我的字符串数组?

而且在活动2我试图用取SharedPreferences值:

 //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显示The value is : [Ljava.lang.String;@424b1370

为何不打印我的字符串数组?

+0

是否要将数组传递给其他活动 –

+0

是的,这正是我想要做的。 – CHarris

+0

检查我下面的答案https://stackoverflow.com/a/46379769/7666442 –

两个活动之间传输的数据,可以使用bundle

为例

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

从包获得的数据是这样的(在你的第二个活动)

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

简单方法希望它能帮助你:)

根据Omar Aflak's comme nt以上:

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