为什么此代码不将我的字符串数组传递给GSON的另一个活动



我正在使用Gsonstring array转移到SharedPreferences的另一个活动中(因为SharedPreferences AS AS PRIST仅适用于字符串,显然)

您能告诉我此代码有什么问题?:

在活动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的评论:

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

最新更新