开发者

Preference Screen as interface for database?

开发者 https://www.devze.com 2023-03-20 11:02 出处:网络
Is it possible to use a preference screen as a simple interface to read and write values to a database?

Is it possible to use a preference screen as a simple interface to read and write values to a database?

Basically, I like the way the preference screen looks and operates, but preferences aren't a suitable way to store all the data I have.

I know how to get it to display correctly, but I'm unsure on how to access the values represented on the screen, and how to keep it from writing a preference file.

Is this even a good 开发者_开发百科idea? Thanks.


Just to follow this up for anyone that is interested. I got it working by using a Preference.OnPreferenceChangeListener() to store the value as a int or string or whatever. For example:

et_model.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener(){
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                String val = (String) newValue;
                preference.setSummary(val);
                model = val;
                return true;
            }
        });

Then once the user presses done, I add the data to the database in the usual way with my SQLight database helper class.

When I load the values from the database, I simply use Preference.SetText(String), and Preference.SetSummary(String).

I guess it is still writing a preference file because if I don't set the preference's text it will load with whatever was set last, but I don't think this is a problem. I could also delete the preference file when I close the activity or something...

If you want a good example, just look at the source for the AlarmClock (now DeskClock) Look at SetAlarm.java and set_alarm.xml for the layout(Save and cancel keys) and alarm_prefs.xml for the actual preference layout.


I don't think that is such a hot idea, especially if you plan on having a tone of data in your database. How ever if you did want to do it, I would just extend the Preference widgets that you will use and have them interface with the database. For example, lets say you have 10 items in a table and you want to select one item (row in the database), you would override the ListPreference and fill it with the content of the applicable database row.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号