Search code examples
androidandroid-preferenceslistpreference

PreferenceActivity not working


I have a PreferenceActivity in my app so that it shows a correct WebView and hide others. It saves the setting that user saved, but it doesn't hide the WebView. This is my code.

Preference.xml

<PreferenceCategory android:title="@string/setDefaultDic">
    <ListPreference
        android:defaultValue="naver"
        android:dialogTitle="@string/defaultDic"
        android:entries="@array/defaultDic"
        android:entryValues="@array/defaultDic_values"
        android:key="defaultDic"
        android:title="@string/defaultDic"
        android:summary="@string/summaryDefaultDic"/>
</PreferenceCategory>

This is my PreferenceActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.pref_settings);
    setOnPreferenceChange(findPreference("defaultDic"));
}

and my MainActivity.java.

private void loadPreferences()
{
    web1 = (WebView) findViewById(R.id.web1);
    web2 = (WebView) findViewById(R.id.web2);
    web3 = (WebView) findViewById(R.id.web3);
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String defaultDicLoad;
    defaultDicLoad = sharedPrefs.getString("defaultDic","naver");
    if (defaultDicLoad == "naver"){
        web2.setVisibility(View.GONE);
        web3.setVisibility(View.GONE);
    }
    else if (defaultDicLoad == "daum"){
        web1.setVisibility(View.GONE);
        web3.setVisibility(View.GONE);
    }
    else if (defaultDicLoad == "google"){
        web1.setVisibility(View.GONE);
        web2.setVisibility(View.GONE);
    }
}

in onCreate, there is loadPreferences() so that it hides WebView.


Solution

  • You're checking for String equality incorrectly. You should use the equals() method instead of ==. For example, instead of

    if (defaultDicLoad == "naver")
    

    You should use

    if ("naver".equals(defaultDicLoad))
    

    This applies to all your String equality checks.