PreferenceActivity display/edit the values of a domain object

884 views Asked by At

Given this code, how do I get the values from the checkbox- and textpreference and store them in the domain object?

public class MonitorPreferences extends PreferenceActivity {
    private PersistenceManager pm;
    private Monitor monitor;
    private boolean mActive;
    private String mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pm = new PersistenceManager(getApplicationContext());
        addPreferencesFromResource(R.xml.monitors_pref);

        fetchDomainObject();
    }

    private void fetchDomainObject() {  
        monitor =  pm.fetchMonitor(getIntent().getLongExtra(SuperListActivity.EXTRA_KEY_MONITOR_ID, -1));
    }

    private void persistDomainObject(Monitor monitor) {
        pm.persist(monitor);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="General">
        <CheckBoxPreference
            android:key="active_chkbox"
            android:title="Active"
            android:defaultValue="true"
            android:persistent="false"/>
        <EditTextPreference
            android:key="name_txt"
            android:dependency="active_chkbox"
            android:title="Name"
            android:summary="Enter a name"
            android:dialogTitle="Enter a name"
            android:dialogMessage="Enter a name"
            android:defaultValue="John Doe"
            android:persistent="false"/>
    </PreferenceCategory>
</PreferenceScreen>

Original question: Creating a normal Activity with the look and feel of a PreferenceActivity My goal is to edit the variables of a domain object from an Activity with the look and feel of stock android preferences. What's the easiest way of accomplishing this?

Would it be possible to create a a PreferenceActivity and somehow modify it to display/edit the values of a domain object instead of the values from SharedPreferences?

1

There are 1 answers

1
Vladimir Ivanov On BEST ANSWER

Of course. Just add the code to store the values into the database and don't forget to mension android:persistent=false attribute in your preferences xml.

final CheckBoxPreference soundcb = (CheckBoxPreference) findPreference("active_chkbox");

Further you can do many things with soundcb object: read the value, set onClickListeners and so on.