2015-01-21 2 views
0

일부 키 - 값 쌍이 이미 공유 환경 설정에 있는지 여부를 알고 싶습니다.SharedPreferences 작업

sharedPref=getApplicationContext().getSharedPreferences("sharedf",Context.MODE_PRIVATE); 
    editor=sharedPref.edit(); 
    editor.putString("secretKey",e1.getText().toString()); 
    editor.commit(); 

공유 키가 이미 존재하는지 확인하고 싶습니다.

+0

많은 고맙습니다.이 방법을 구현하려고합니다. 작동하지 않으면 알려 드리겠습니다. :) –

+0

그리고이 키 값 쌍에 대한 결과를 검색하는 방법 ?? –

답변

0

당신은 다음 방법으로

sharedPref=getApplicationContext().getSharedPreferences("sharedf",Context.MODE_PRIVATE); 
String secretKey = sharedPref.getString("secretKey", null); 
if (null == secretKey) { 
    // not exists 
} else { 
    // exists 
} 
0
 if (mPrefs.contains(SETTING_THEME)) { 
       // read the value 
       mThemeSettings = mPrefs.getString(SETTING_THEME, THEME_KEY_DEFAULT); 
      } 
    // create a new "prefs entry" (first start, after installing the app) 
    else mEditor.putString(SETTING_THEME, mThemeSettings).apply(); 
0

android: check if value exists in Shared Preferences

시도가 (문자열 키)를 포함 길봐 Accorting하는 방법이있다 Javadocs에,

환경 설정에 환경 설정이 포함되어 있는지 확인합니다. 환경 설정이 환경 설정에있는 경우는 true, 그렇지 않은 경우는 false를 리턴합니다.