在Android中,数据存储是开发人员不可以避免的。Android为开发者提供了很多的存储方法,在前面的博客中,已经讲述了sqlite存储数据。今天将介绍用SharedPreferences来存储数据,它可以将数据保存在应用软件的私有存储区,存储区的数据只能被写入这些数据的软件读取。SharedPreference通过键值对的方法存储数据。

1.SharedPreference存储简单数据

SharedPreference可以存放简单的String、Boolean、Int等对象。

 <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名" /> <EditText
android:id="@+id/editename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView1" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editename"
android:layout_below="@+id/editename"
android:layout_marginTop="15dp"
android:text="兴趣爱好" /> <EditText
android:id="@+id/editehoby"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="21dp"
android:ems="10" > <requestFocus />
</EditText> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editehoby"
android:layout_below="@+id/editehoby"
android:layout_marginTop="18dp"
android:text="是否工作" /> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="22dp"
android:text="单位性质" /> <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="24dp" > <RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="国营" /> <RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="私营" /> <RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="股份制" />
</RadioGroup> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/radioGroup1"
android:layout_alignRight="@+id/editehoby"
android:layout_marginBottom="26dp"
android:layout_marginRight="30dp"
android:text="tiao" /> </RelativeLayout>

main.xml

这里定义了几个edittext。

 protected void onStop()
{
//获得SharedPreference对象
SharedPreferences myShared=getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
//获得editor对象
SharedPreferences.Editor editor=myShared.edit();
//添加需要保存的数据
editor.putString("name", edname.getText().toString());
editor.putString("hobby", edhoby.getText().toString());
editor.putBoolean("employee", cbcareer.isChecked());
editor.putInt("companytype", rdCompanyType.getCheckedRadioButtonId());
//提交数据
editor.commit();
super.onStop(); }

保存数据

这对数据的存储,并没有放在单独的事件中,而是放在onstop方法中。当activity停止的时候,会自动提交数据。

     protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edname=(EditText)findViewById(R.id.editename);
edhoby=(EditText)findViewById(R.id.editehoby);
cbcareer=(CheckBox)findViewById(R.id.checkBox1);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(MainActivity.this, ProductSharedActivity.class);
startActivity(intent);
}
});
rdCompanyType=(RadioGroup)findViewById(R.id.radioGroup1);
SharedPreferences sharedpre=getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
edname.setText(sharedpre.getString("name", ""));
edhoby.setText(sharedpre.getString("hobby", ""));
cbcareer.setChecked(sharedpre.getBoolean("employee",false));
rdCompanyType.check(sharedpre.getInt("companytype", -1)); }

数据读取

数据读取与保存的方法类似。

2.SharedPreference保存复杂数据

SharedPreference不仅可以保存简单的数据,而且可以保存复杂的数据对象,比如对象、图像等。保存复杂的数据类型,需要对数据进行编码。对数据的保存方法和上面的基本一致

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:text="产品ID" /> <EditText
android:id="@+id/txtID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="25dp"
android:ems="10" > <requestFocus />
</EditText> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtID"
android:layout_below="@+id/txtID"
android:layout_marginTop="18dp"
android:text="产品名称" /> <EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="32dp"
android:ems="10" /> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtName"
android:layout_centerVertical="true"
android:text="产品价格" /> <EditText
android:id="@+id/txtprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="28dp"
android:ems="10" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtprice"
android:layout_toRightOf="@+id/textView3" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/imageView1"
android:layout_marginTop="34dp"
android:text="选择图像" /> </RelativeLayout>

base.xml

后台代码:

     protected void onStop()
{
Product product=new Product();
/*product.setID(edid.getText().toString());
product.setName(edname.getText().toString());
product.setPrice(edprice.getText().toString());*/
product.productname=edname.getText().toString();
product.productid=edid.getText().toString();
product.productprice=edprice.getText().toString(); ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos;
((BitmapDrawable)imageview.getDrawable()).getBitmap().compress(CompressFormat.JPEG, 50, baos);
String imagebase=new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(product);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
//获得SharedPreference对象
SharedPreferences myshared=getSharedPreferences("base64", Activity.MODE_PRIVATE);
String productbase=new String(Base64.encode(baos.toByteArray(),Base64.DEFAULT));
//获得editor对象
SharedPreferences.Editor editor=myshared.edit();
//添加需要存储的数据
editor.putString("product", productbase);
editor.putString("productimage", imagebase);
//提交保存数据
editor.commit(); super.onStop();
}

数据保存

这里需要保存的数据都经过了base64的编码处理,在编码之前需要将其转为流的形式。

Android学习之简单的数据存储的更多相关文章

  1. Android系统的五种数据存储形式(二)

    之前介绍了Android系统下三种数据存储形式,今天补充介绍另外两种,分别是内容提供者和网络存储.有些人可能认为内存提供者和网络存储更偏向于对数据的操作而不是数据的存储,但这两种方式确实与数据有关,所 ...

  2. Android系统的五种数据存储形式(一)

    Android系统有五种数据存储形式,分别是文件存储.SP存储.数据库存储.contentprovider 内容提供者.网络存储.其中,前四个是本地存储.存储的类型包括简单文本.窗口状态存储.音频视频 ...

  3. Android开发7:简单的数据存储(使用SharedPreferences)和文件操作

    前言 啦啦啦~大家好,又见面啦~ 本篇博文讲和大家一起完成一个需要注册.登录的备忘录的,一起学习 SharedPreferences 的基本使用,学习 Android 中常见的文件操作方法,复习 An ...

  4. Android中的5种数据存储方式

    本文转自  http://hi.baidu.com/maguowei/blog/item/7aca46c25574a33ae5dd3ba4.htmlAndroid数据存储Android提供了5种方式存 ...

  5. android学习经常使用的数据文件夹

    android工程实践 1.仿360一键清理实现(一) "一键清理"是一个桌面图标,点击图标后,显示一个视图.进行清理动画.之后显示清理了几个进程,释放了多少M内存.点击" ...

  6. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  7. Android学习--跨程序共享数据之内容提供其探究

    什么是内容提供器? 跨程序共享数据之内容提供器,这是个什么功能?看到这个名称的时候最能给我们提供信息的应该是“跨程序”这个词了,是的重点就是这个词,这个内容提供器的作用主要是用于在不同的引用程序之间实 ...

  8. C#图解教程学习笔记——数据类型与数据存储

    一.数据类型1. 预定义类型C#提供16种预定义类型,包括13种简单类型和3种非简单类型:(1)简单类型<1>11种数值类型: 不同长度的有符号和无符号整数类型 浮点数的float和dou ...

  9. Android学习笔记_8_使用SharedPreferences存储数据

    1.SharedPreferences介绍: Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPrefer ...

随机推荐

  1. hive优化之自己主动合并输出的小文件

    1.先在hive-site.xml中设置小文件的标准. <property> <name>hive.merge.smallfiles.avgsize</name> ...

  2. Android 四大组件之 Activity

    1 简介 Activity (活动) 即应用程序 显示的 界面.可以通过两种方式 设置显示的内容 1:纯代码方式 2:xml 布局方式 无论哪一种方式,都是通过 setContentView 来设置显 ...

  3. [Python笔记][第一章Python基础]

    2016/1/27学习内容 第一章 Python基础 Python内置函数 见Python内置函数.md del命令 显式删除操作,列表中也可以使用. 基本输入输出 input() 读入进来永远是字符 ...

  4. SERVERPROPERTY方法说明

    SERVERPROPERTY 返回有关服务器实例的属性信息. 语法 SERVERPROPERTY ( propertyname ) 参数 propertyname 是包含要返回的服务器属性信息的表达式 ...

  5. S - stl 的mapⅠ

    先来介绍一下stl中的map这个功能 头文件#include<map> map是STL的一个关联容器,它提供一对一的数据处理能力 就像一个人对应一个编号一样 定义 为  map<in ...

  6. Velocity中避免null引起的数据问题

    请先看下面一段代码: #foreach($id in [1..50]) #set($user = $User.Get($id)) $id : ${user.name} #end 上面这段代码中,假设只 ...

  7. HTML5的结构学习(2) --- 新增的非主体结构元素

    除了上一篇学习到的主体结构元素之外,html5还增加了一些表示逻辑结构和附加信息的非主体结构元素: 1.header 解释:一种具有引导和导航作用的结构元素. 用途:通常用来放置整个页面或者页面内某一 ...

  8. python核心编程-第五章-习题

    1.长整型表示数的范围比整型更大.在python中,整型.长整型趋于统一,普通用户不用特别关注两者区别,仅当需引用C语言时需要特别注意. 2.操作符 (a) def product(x,y): ret ...

  9. 【CKEditor ASP.NET】解决360安全浏览器极速模式下不显示

    博主问题只是出在误删了style.js文件 首先我用的是这种模式,在单个页面上导入: <%@ Register Assembly="CKEditor.NET" Namespa ...

  10. VS2010中添加dll目录

    RT,比如用VS写QT,用qmake生成的项目,需要在项目属性里设置:调试->环境,path=%path%;C:\Qt\4.8.5\bin 这样省的每次都要把一堆dll复制到debug/rele ...