2016-12-31 1 views
1

처음에는 배열 목록이었습니다. 그래서 그것을 세트로 변경해 보았습니다. 그래서 putStringSet에 놓을 수있었습니다. 하지만 그래 .. 문자열 데이터 형식이 없습니다비 문자열 데이터 타입을 putStringSet에 두십시오. SharedPreferences 안드로이드

Set<RecordData> set = new HashSet<>(dataList); 
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putStringSet("recordlists", set); 

RecordData는 내가 만든 클래스입니다.

나는 빨기.

+1

중복 - HTTP :

public static void saveSharedPreferencesLogList(Set<RecordData> record) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); SharedPreferences.Editor prefsEditor = preferences.edit(); Gson gson = new Gson(); String json = gson.toJson(record); prefsEditor.putString("myJson", json); prefsEditor.commit(); } public static Set<RecordData> loadSharedPreferencesLogList() { Set<RecordData> record = new HashSet<>(dataList); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); Gson gson = new Gson(); String json = preferences.getString("myJson", ""); if (json.isEmpty()) { record = new Set<RecordData>(); } else { Type type = new TypeToken<Set<RecordData>>() { }.getType(); record = gson.fromJson(json, type); } return record; } 

은 또한 당신의 Gradle을 파일이 배치 // 유래 .com/questions/7057845/save-arraylist-to-sharedpreferences –

+0

당신은 그것을 세트로 변환하는 것이 맞지만, 잘못된 방식으로 저장하고 있습니다. 위에서 언급 한 링크를 확인하십시오. –

+0

Gson 라이브러리를 사용하여 사용자 지정 개체를 저장하고 검색 할 수도 있습니다 - 여기에서 설명합니다 - http://stackoverflow.com/a/36184406/3225001 –

답변

0

당신이 GSON 라이브러리를 사용할 수 있습니다 .. 는 저장 및로드 공유 환경 설정 데이터의 경우이 시도 :

compile 'com.google.code.gson:gson:2.6.2' 
관련 문제