简体   繁体   English

Android:保持变量类似于数据库

[英]Android: keeping variables similar to a database

I have an android app that has a database of People . 我有一个具有People数据库的android应用程序。 When I close the application, the data base remains and when I turn off the phone and turn it back on, the People are all there on my list. 当我关闭应用程序时,数据库仍然存在,当我关闭手机然后重新打开时, 人员就在列表中。

My app supports other features such as letting the user decide the Person should be displayed. 我的应用程序支持其他功能,例如让用户决定应显示此 Eg, FirstName then LastName or vice versa etc. When I set these settings then quit the app and start it again. 例如,名字,然后姓氏,反之亦然,等等。当我设置这些设置后,退出应用程序并重新启动。 The settings are lost. 设置丢失。

How do I keep these variables set? 如何设置这些变量? Kind of like they are in the database. 有点像它们在数据库中。 What is the easiet/best way? 最简单/最好的方法是什么?

Thank you. 谢谢。

Use SharedPreferences 使用SharedPreferences

Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). 用于访问和修改由getSharedPreferences(String,int)返回的首选项数据的接口。 For any particular set of preferences, there is a single instance of this class that all clients share. 对于任何特定的首选项集,所有客户端都共享一个此类的单个实例。 Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage. 对首选项的修改必须通过SharedPreferences.Editor对象,以确保首选项值保持一致状态并控制将它们提交到存储时的状态。 Objects that are returned from the various get methods must be treated as immutable by the application. 从各种get方法返回的对象必须被应用程序视为不可变的。

How to use SharedPreferences in Android to store, fetch and edit values 如何在Android中使用SharedPreferences来存储,获取和编辑值

If your dataset is small, you can use SharedPreferences . 如果数据集较小,则可以使用SharedPreferences Or if your dataset is huge, you should use SQLiteDatabase . 或者,如果您的数据集很大,则应使用SQLiteDatabase

getSharedPreferences("name", Context.MODE_PRIVATE).edit().putString("my_display_toggle", myDisplayToggle).commit(); 
Loading the value: getSharedPreferences("name", Context.MODE_PRIVATE).getString("my_display_toggle", "default_value");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM