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

时间:2017-09-23 12:39:36

标签: android gson sharedpreferences

我正在使用Gsonstring array转移到另一个SharedPreferences的活动(因为SharedPreferences因为只有字符串效果最好,显然)

你能告诉我这段代码有什么问题吗?:

在活动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

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

2 个答案:

答案 0 :(得分:0)

在两个活动之间传输数据的简单方法,您可以使用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");

希望它会对你有所帮助:)。

答案 1 :(得分:0)

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