[Andriod官方训练教程]保存数据之保存键-值对的集合

原文地址:https://developer.android.com/training/basics/data-storage/shared-preferences.html

-------------------------------------------------------------------------------------------------------------------------------

If you have a relatively small collection of key-values that you'd like to save, you should use theSharedPreferences APIs. ASharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. EachSharedPreferences file is managed by the framework and can be private or shared.

如果你有一个相对较小的键-值对集合想要存储,你应该使用SharedPreferences APIs。一个SharedPreferences对象指向一个包含键值对的文件,并且提供了一些简单的方法来进行读取和写入它们。每个SharedPreferences文件可以是私有的或是共享的,靠framework来管理它们。

This class shows you how to use the SharedPreferences APIs to store and retrieve simple values.

这节课演示如何使用SharedPreferences APIs来存储和检索简单的值。

Note: The SharedPreferences APIs are only for reading and writing key-value pairs and you should not confuse them with thePreference APIs, which help you build a user interface for your app settings (although they useSharedPreferences as their implementation to save the app settings). For information about using thePreference APIs, see theSettings guide.

注意:SharedPreferences APIs不仅仅可以读取和写入键值对,而且你不应该把它们和Preference APIs相混淆,后者帮助你为你的app设置建立一个用户接口(尽管在存储app设置时它们使用SharedPreferences实现)。

 

Get a Handle to a SharedPreferences ——得到一个SharedPreferences的句柄


You can create a new shared preference file or access an existing one by calling one of two methods:

  • getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from anyContext in your app.
  • getPreferences() — Use this from anActivity if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.

你可以通过调用下面两个方法之一创建一个新的共享引用文件或进入一个已经存在的文件:

  • getSharedPreferences()——如果你需要多个 依靠名字(第一个参数)定义的共享引用文件,你可以使用这个函数。你可以从你的app中的任意Context里调用它。
  • getPreferences()——如果你在这个activity中仅仅需要使用一个共享应用文件,就使用这个方法。因为它检索一个属于这个activity的默认共享应用文件,你不需要提供一个名称。

For example, the following code is executed inside a Fragment. It accesses the shared preferences file that's identified by the resource stringR.string.preference_file_key and opens it using the private mode so the file is accessible by only your app.

例如,下面的代码在一个Fragment中执行。它依靠资源字符R.string.preference_file_key定义来访问一个共享应用文件,然后使用私有模式打开它,以使文件仅仅对你的app是可访问的。

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
        getString(R.string.preference_file_key), Context.MODE_PRIVATE);

When naming your shared preference files, you should use a name that's uniquely identifiable to your app, such as"com.example.myapp.PREFERENCE_FILE_KEY"

Alternatively, if you need just one shared preference file for your activity, you can use thegetPreferences() method:

当给你的共享引用文件命名时,你应该使用一个唯一的、可确认的名字,例如“com.example.myapp.PREFERENCE_FILE_KEY”。或者,如果你仅仅需要一个共享引用文件,你可以使用getPreferences()方法。

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

Caution: If you create a shared preferences file withMODE_WORLD_READABLE orMODE_WORLD_WRITEABLE, then any other apps that know the file identifier can access your data.

警告:如果你使用MODE_WORLD_READABLE 或者MODE_WORLD_WRITEABLE ,那么其他任何apps都知道这个文件标识符可以访问你的数据。

 

Write to Shared Preferences —— 向共享引用写入数据


To write to a shared preferences file, create a SharedPreferences.Editor by callingedit() on yourSharedPreferences.

为了向一个共享的应用文件写入,通过在你的SharedPreferences调用edit() 来创建一个SharedPreferences.Editor

Pass the keys and values you want to write with methods such as putInt() andputString(). Then callcommit() to save the changes. For example:

利用putInt() 和putString()这样的方法传递你想要写入的键和值。然后调用commit()来存储变化。例如:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

 

Read from Shared Preferences —— 从共享引用中读取数据


To retrieve values from a shared preferences file, call methods such as getInt() andgetString(), providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:

为了从一个共享引用文件中检索数值,调用getInt() andgetString()等方法,提供一个你想到得到的键,和一个供选择的默认返回值(如果这个键不存在的话)。例如:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本项目是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。该系统主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的Java学习者,包含项目源码、数据库脚本、项目说明等,有论文参考,可以直接作为毕设使用。 后台框架采用SpringBoot,数据库使用MySQL,开发环境为JDK、IDEA、Tomcat。项目经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。 该系统的功能主要包括商品管理、订单管理、用户管理等模块。在商品管理模块中,可以添加、修改、删除商品信息;在订单管理模块中,可以查看订单详情、处理订单状态;在用户管理模块中,可以注册、登录、修改个人信息等。此外,系统还提供了数据统计功能,可以对销售数据进行统计和分析。 技术实现方面,前端采用Vue框架进行开发,后端使用SpringBoot框架搭建服务端应用。数据库采用MySQL进行数据存储和管理。整个系统通过前后端分离的方式实现,提高了系统的可维护性和可扩展性。同时,系统还采用了一些流行的技术和工具,如MyBatis、JPA等进行数据访问和操作,以及Maven进行项目管理和构建。 总之,本系统是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。系统经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值