How to force the value of listPreference to reset its value?

I have a ListPreference and it has a default value and it works fine. I also have a preference that resets all general settings that work fine. The problem is that if you return to ListPreference, it will still show the old value. If you completely exit the settings and come back, it displays correctly.

So the question is, how can I get ListPreference to use the correct value? It seems that all other preferences are working fine.

I found a job, but I am not happy with this, as it seems dirty to me. When the user clicks on reset, after the values โ€‹โ€‹were reset, I myself set the value for the value that it is already set.

( (ListPreference) findPreference( myListKey ) ).setValue( String.valueOf( DEFAULT_VALUE ) );

Thanks in advance.

+5
source share
5 answers

I know him late, but maybe someone else stumbled upon this, maybe find it useful.

Preference reset = (Preference) findPreference("reset");
reset.setOnPreferenceClickListener(new OnPreferenceClickListener() {

    public boolean onPreferenceClick(Preference preference) {
    // TODO Auto-generated method stub
    preference.getEditor().clear().commit();
        Toast.makeText(getBaseContext(),
                "Restored to default settings", Toast.LENGTH_SHORT)
                .show();
        return false;
        }
    });
+1
source

Azizi Musa on something, but null throws an exception.

However, this worked:

ListPreference yeeha = (ListPreference) findPreference ("yeeha");

yeeha.setValue("");
+1
source

, onSetInitialValue ListPreference. .

public class CustomListPreference ListPreference {

protected void onSetInitialValue (boolean restoreValue, Object defaultValue) {

//, (restoreValue) {

} { // defautlValue

}

}

}

0
ListPreference pref = (ListPreference) findPreference ("yeeha");

pref.setValue(null);
0

, ListPreference. , setEntries ( ).

DrawerListener, My PreferenceFragment, .

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

final MainOptionsFragment fragment = (MainOptionsFragment) getFragmentManager().
            findFragmentById(R.id.fragment_drawer);

mDrawerOptions = new DrawerLayout.SimpleDrawerListener() {
    @Override
    public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
        fragment.onOpened();
    }
};

mDrawerLayout.addDrawerListener(mDrawerOptions);

onPrepareDialogBuilder, , .

class CustomListPreference extends ListPreference {
    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        // TODO setEntries
        super.onPrepareDialogBuilder(builder);
    }
}
0

All Articles