如何在Android中向Spinner添加sharedpreference数据?

时间:2018-10-01 04:19:25

标签: android sharedpreferences android-spinner

在我的BaseActivity中,我使用改型获取JSON响应并将其存储在sharedpreference中。在JSON响应中包含多个列表,我想从中获取特定列表并添加到下拉列表中。

在sharedpreference中存储JSON响应

 SharedPreferences sharedpreferences = getSharedPreferences("GenderResponse", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                Gson gson = new Gson();
                String json = gson.toJson(heroList);
                editor.putString("genderResponse",json);
                Log.d("gson","gson"+json);
                editor.commit();

通过片段中的sharedpreference检索数据。

 SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("GenderResponse",Context.MODE_PRIVATE);
//        genderResponse2 = Collections.singletonList(sharedPreferences.getString("genderResponse", ""));

        Gson gson = new Gson();
        String json = sharedPreferences.getString("genderResponse","");
        Example example = gson.fromJson(json,Example.class);
        **genderResponse2 = Collections.singletonList(example.getMeetmeConfig().getConfigContent().getGender());**
        Log.v("genderResponse","genderResponse"+genderResponse2);
        ArrayAdapter<CharSequence> myAdapter = new ArrayAdapter<CharSequence>(this.getActivity(),android.R.layout.simple_expandable_list_item_1,genderResponse2);
        Log.e("myAdapter","myAdapter:"+myAdapter);
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        genderDropDown.setAdapter(myAdapter);

数据以微调框显示,但以json格式显示,请以任何方式进行指导。    JSON响应

{
    "statusCode": "200",
    "statusMessage": "SUCCESS",
    "meetmeConfig": {
        "id": "bb52dc0f-29d0-4079-99c7-a07c8045a829",
        "moduleName": "MeetMe",
        "createdDate": 1523962430721,
        "configContent": {
            "trackingOptions": [
                {
                    "optionName": "Before the meet",
                    "isTimeRequired": true,
                    "trackingTime": [
                        5,
                        10,
                        15
                    ],
                    "isDeleted": false
                },
                {
                    "optionName": "After the meet",
                    "isTimeRequired": true,
                    "trackingTime": [
                        5,
                        10,
                        15
                    ],
                    "isDeleted": false
                },
                {
                    "optionName": "At the start",
                    "isTimeRequired": false,
                    "trackingTime": [],
                    "isDeleted": false
                },
                {
                    "optionName": "Never",
                    "isTimeRequired": false,
                    "trackingTime": [],
                    "isDeleted": false
                }
            ],
            "numberOfParticipants": 8,
            "mapResetTimeInterval": 30,
            "meetingTrackableTime": 3600,
            "addressTypes": [
                "Home",
                "Work"
            ],
            "transportModes": [
                "Walking",
                "Driving"
            ],
            "gender": [
                "Male",
                "Female",
                "Trans-Female",
                "Bi-Gender",
                "Non-Binary",
                "Gender nonconfirming",
                "Undisclosed",
                "Rather not say"
            ],
            "toastDelayTimeForPulse": 3,
            "syncToastMaxTimeInterval": 300,
            "syncToastThirdTimeInterval": 180,
            "firstTimeInterval": 1,
            "secondTimeInterval": 2,
            "meetmeSearchContactTimeInterval": 2,
            "signupToastDelayTime": 4,
            "signupToastdelatimebysix": 6,
            "signupToastDelayMedium": 5,
            "profileToastDelayTime": 4,
            "profileToastDelatimebysix": 6,
            "languages": [
                "English",
                "Spanish",
                "Marathi",
                "Hindi",
                "Bengali",
                "French",
                "Arabic",
                "German",
                "Italian",
                "Dutch",
                "Japanese",
                "Russia",
                "Korean"
            ]
        }
    }
}

0 个答案:

没有答案