Itcast_android学习_数据存储于访问:SharedPreferences
来源:互联网 发布:创别书城app下载软件 编辑:程序博客网 时间:2024/06/07 22:19
1、通过上下文对象得到SharedPreferences对象,要注意的是指定xml文件名,不要添加.xml后缀
SharedPreferences preferences = context.getSharedPreferences("itcast", Context.MODE_PRIVATE);
2、取得编辑器对象后,再添加数据
Editor editor = preferences.edit();editor.putString("name", name);editor.putInt("age", age);//数据存放在内存中,还没有保存到文件中//保存到xml文件中editor.commit();//把数据提交回文件中
3、如何将list数据添加到SharedPreferences
/** * 保存List数据 * @return */public boolean saveArray() {SharedPreferences preferences = context.getSharedPreferences("itcast", Context.MODE_PRIVATE);Editor editor= preferences.edit();List<String> lists = new ArrayList<String>();for(int i=0;i<10;i++){lists.add("apple"+i);//向list数组中添加数据}editor.putInt("size",lists.size()); for(int i=0;i<lists.size();i++) { editor.putString("key_" + i, lists.get(i));//添加list数据到SharedPreferences } return editor.commit(); }
4、数据保存后,会提交到itcast.xml文件中,文件默认保存位置:data/data/<package name>/itcast.xml,如图所示:
5、我们可以通过看itcast.xml文件,知道其保存数据的格式和器保存的数据
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map><string name="name">andy</string><int name="age" value="22" /></map>
6、以上是将数据保存到SharedPreferences对象当中,那么如何获取到SharedPreferences对象里的数据了?
Map<String,String> maps = new HashMap<String, String>();SharedPreferences preferences = context.getSharedPreferences("itcast", Context.MODE_PRIVATE);//通过 preferences.getString("name", "")来获取SharedPreferences里的属性值maps.put("name", preferences.getString("name", ""));//如果没有name属性,则返回默认值"";maps.put("age", String.valueOf(preferences.getInt("age", 0)));
7、下面将完整的源代码贴出来:
PreferenceService.java文件
package cn.itcast.Service;import java.util.HashMap;import java.util.Map;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;public class PreferenceService {private Context context ;public PreferenceService(Context context) {this.context = context;}/** * 保存参数 * @param name * @param age */public void save(String name, Integer age){SharedPreferences preferences = context.getSharedPreferences("itcast", Context.MODE_PRIVATE);Editor editor = preferences.edit();editor.putString("name", name);editor.putInt("age", age);editor.commit();}/** * 获取各项配置参数 * @return */public Map<String,String> getPreferences(){Map<String,String> maps = new HashMap<String, String>();SharedPreferences preferences = context.getSharedPreferences("itcast", Context.MODE_PRIVATE);maps.put("name", preferences.getString("name", ""));//如果没有name属性,则返回默认值"";maps.put("age", String.valueOf(preferences.getInt("age", 0)));return maps;}}
SharedPreferencesActivity.java文件
package cn.itcast.shared;import java.util.Map;import cn.itcast.Service.PreferenceService;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class SharedPreferencesActivity extends Activity { private EditText nameText; private EditText ageText; private PreferenceService service; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nameText = (EditText) this.findViewById(R.id.name); ageText = (EditText) this.findViewById(R.id.age); //实例化业务bean service = new PreferenceService(this); Map<String,String> params = service.getPreferences(); nameText.setText(params.get("name"));//数据回显 ageText.setText(params.get("age")); } //对于的button设置了android:onClick="save"属性,单击button之后执行保存操作 public void save(View v){ String name = nameText.getText().toString(); String age = ageText.getText().toString(); //执行保存操作 service.save(name ,Integer.valueOf(age)); Toast.makeText(getApplicationContext(), R.string.success, 1).show(); }}
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/name" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/name" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/age" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:numeric="integer" android:id="@+id/age" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:onClick="save"/></LinearLayout>
- Itcast_android学习_数据存储于访问:SharedPreferences
- Android数据存储和访问之SharedPreferences存储方式_用户登录记住密码案例
- Android数据存储和访问(SharedPreferences存储)
- Android开发_数据存储之SharedPreferences
- Android 数据存储与访问 SharedPreferences
- 使用SharedPreferences进行数据存储与访问
- [Android SQLite]数据存储与访问 - SharedPreferences
- 使用SharedPreferences进行数据存储并访问
- android:数据的存储与访问----Sharedpreferences
- Android学习 —— 数据的存储与访问方式二: SharedPreferences(偏好参数保存)
- SharedPreferences存储数据 基本学习实例
- android 数据存储SharedPreferences 学习笔记
- Android学习之SharedPreferences数据存储
- android数据存储与访问之使用SharedPreferences
- android数据存储与访问之使用SharedPreferences
- 数据存储与访问之SharedPreferences(偏好参数保存)
- 访问SharedPreferences中的数据
- 数据存储SharedPreferences
- android logcat无法打印日志(华为手机)
- Objetc中内部锁的应用及获取
- 第四章(1)Libgdx项目安装、运行和调试
- 10271 - Chopsticks
- 仿射变换
- Itcast_android学习_数据存储于访问:SharedPreferences
- linux内存管理浅析
- Gallery of Processor Cache Effects
- 回顾Eurosys 2013、HPDC 2013、Euro-par 2013的论文投稿经历、经验总结帖
- 浅谈C/C++中的static和extern关键字
- hdu1701(数学 浮点数)
- UVa 10167Birthday Cake
- 网页中的:代码编写可变长宽的 框框 背景图片 制作 可变适应性的自动改变长和宽的
- ExecutorService