在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. 【极客学院出品】Cocos2d-X系列课程之九-BOX2D物理引擎

    Cocos2d-x 是时下最热门的手游引擎,在国内和国外手机游戏开发使用的份额各自是70%和25%,在App Store的top10中,有7个是用它开发的. 本节课程为Cocos2d-x系列课程之九, ...

  2. C#入门(一):IDE

    设计流程 .NET可视化对象 创建工程的时候,会创建三个文件 Form1.cs Form1.Designer.cs Program.cs 当增加一个控件的时候,会在Form1.Designer.cs增 ...

  3. NET基础课--应用程序编译和执行1

  4. 【27前端】在线css三角

    我们都知道利用css边框的属性可以画出三角形,这里为了方便,我做了一个简单的demo页面供大家使用. 在线css三角

  5. 解决net-snmp正确输出MAC地址和判断空的IP地址

    function readVarbinds (buffer, varbinds) { buffer.readSequence (); while (1) { buffer.readSequence ( ...

  6. linq读书笔记3-操作符之select与selectmany

    linq对数据的查询方式的表达形式主要有两种: var demo =from p in pList where p.id=*** select p; var demo =pList.where(p=& ...

  7. 【转】VS2010中文注释带红色下划线的解决方法

    转载自:http://blog.csdn.net/whatday/article/details/7856323 环境:Visual Studio 2010 问题:代码中出现中文后会带下划线,很多时候 ...

  8. const参数,const返回值与const函数

    在C++程序中,经常用const 来限制对一个对象的操作,例如,将一个变量定义为const 的: const  int  n=3; 则这个变量的值不能被修改,即不能对变量赋值. const 这个关键字 ...

  9. AngularJS 中的 Promise 和 设计模式

    解决 Javascript 异步事件的传统方式是回调函数:调用一个方法,然后给它一个函数引用,当这个方法完结的时候执行这个函数引用. <!-- lang: js --> $.get('ap ...

  10. tomcat组成及原理[转]

    Tomcat安装好后打开目录;可以看到如下结构: bin :存放服务器脚本; conf :存放配置文件; lib :存放需要的JAR文件; wabapps :存放需要发布的Web应用程序及其部署文件; ...