I have a preference activity that has a language
as ListPreference
which displays the available language list. I can fill the list when onCreate is called, but I want to fill the list when the user clicks on it.
this is the java code
:
public class SettingsActivity extends PreferenceActivity implements OnPreferenceClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
addPreferencesFromResource(R.xml.settings);
} catch (Exception e) {
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
if((preference instanceof ListPreference) && (preference.getKey().equals("language"))){
ListPreference lp = (ListPreference)preference;
CharSequence[] entries = { "English", "French" };
CharSequence[] entryValues = {"1" , "2"};
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
return true;
}
return false;
}
}
and this is the settings.xml
(preference) :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings">
<CheckBoxPreference android:key="enabled" android:title="Application Status" android:summary="Enable or disable the application" />
<ListPreference
android:key="language"
android:title="Language"
android:dialogTitle="Application language"
android:summary="Select the Application language"
/>
</PreferenceCategory>
</PreferenceScreen>
I searched but found no result! An exception occurs every time I click on that list.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…