为什么这段代码不能将我的字符串数组传递给Gson的另一个活动?
问题描述:
我使用Gson
到string 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
为何不打印我的字符串数组?
答
两个活动之间传输的数据,可以使用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));
是否要将数组传递给其他活动 –
是的,这正是我想要做的。 – CHarris
检查我下面的答案https://stackoverflow.com/a/46379769/7666442 –