Android - Using PreferenceScreen to display and save settings to/from ContentProvider

Posted by Donal Rafferty on Stack Overflow See other posts from Stack Overflow or by Donal Rafferty
Published on 2010-03-24T11:49:38Z Indexed on 2010/03/24 11:53 UTC
Read the original article Hit count: 927

I have my own custom Content Provider that loads a datasbase which contains the settings information for my application.

I load the settings from the ContentProvider on the creation of my Settings activity.

My Settings activity is made up of a PreferenceScreen and Dialog based EditText's.

The following code shows how I use the preference screen and edit texts.

So as you can see from the first image this works and displays the menu with the information underneath.

The problem is in image two, when I click on a choice in the menu the dialog pops up but it is empty, I would like to be able to load the data from my content provider into the edit text in the dialog, so in image one it shows "Donal" as the user name so in image two "Donal" should also appear in the edit text in the dialog.

I would also like to be able to listen to the OK button in the dialog so when a user changes a setting I can update the data in my content provider.

Can anyone help me with what I'm trying to do?


Code

    // Root
    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

    // Dialog based preferences
    PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
    dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences);
    root.addPreference(dialogBasedPrefCat);

    // Edit text preference
    EditTextPreference editTextPref = new EditTextPreference(this);
    editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference);
    editTextPref.setKey("edittext_preference");
    editTextPref.setTitle(R.string.title_edittext_preference);
    editTextPref.setSummary(name);
    dialogBasedPrefCat.addPreference(editTextPref);

Image One

alt text


Image Two

alt text

© Stack Overflow or respective owner

Related posts about android

Related posts about contentprovider