Android学习之简单的数据存储
在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学习之简单的数据存储的更多相关文章
- Android系统的五种数据存储形式(二)
之前介绍了Android系统下三种数据存储形式,今天补充介绍另外两种,分别是内容提供者和网络存储.有些人可能认为内存提供者和网络存储更偏向于对数据的操作而不是数据的存储,但这两种方式确实与数据有关,所 ...
- Android系统的五种数据存储形式(一)
Android系统有五种数据存储形式,分别是文件存储.SP存储.数据库存储.contentprovider 内容提供者.网络存储.其中,前四个是本地存储.存储的类型包括简单文本.窗口状态存储.音频视频 ...
- Android开发7:简单的数据存储(使用SharedPreferences)和文件操作
前言 啦啦啦~大家好,又见面啦~ 本篇博文讲和大家一起完成一个需要注册.登录的备忘录的,一起学习 SharedPreferences 的基本使用,学习 Android 中常见的文件操作方法,复习 An ...
- Android中的5种数据存储方式
本文转自 http://hi.baidu.com/maguowei/blog/item/7aca46c25574a33ae5dd3ba4.htmlAndroid数据存储Android提供了5种方式存 ...
- android学习经常使用的数据文件夹
android工程实践 1.仿360一键清理实现(一) "一键清理"是一个桌面图标,点击图标后,显示一个视图.进行清理动画.之后显示清理了几个进程,释放了多少M内存.点击" ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android学习--跨程序共享数据之内容提供其探究
什么是内容提供器? 跨程序共享数据之内容提供器,这是个什么功能?看到这个名称的时候最能给我们提供信息的应该是“跨程序”这个词了,是的重点就是这个词,这个内容提供器的作用主要是用于在不同的引用程序之间实 ...
- C#图解教程学习笔记——数据类型与数据存储
一.数据类型1. 预定义类型C#提供16种预定义类型,包括13种简单类型和3种非简单类型:(1)简单类型<1>11种数值类型: 不同长度的有符号和无符号整数类型 浮点数的float和dou ...
- Android学习笔记_8_使用SharedPreferences存储数据
1.SharedPreferences介绍: Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPrefer ...
随机推荐
- 一种基于Qt的可伸缩的全异步C/S架构server实现(二) 网络传输
二.网络传输模块 模块相应代码命名空间 (namespace ZPNetwork) 模块相应代码存储目录 (\ZoomPipeline_FuncSvr\network) 2.1 模块结构 ...
- ThinkPHP中的__initialize()和类的构造函数__construct()
ThinkPHP中的__initialize()和类的构造函数__construct()网上有很多关于__initialize()的说法和用法,总感觉不对头,所以自己测试了一下.将结果和大家分享.不对 ...
- 【贪心】【Uva11292】 勇者斗恶龙
直接用白书上的翻译吧 例题1 勇者斗恶龙(The Dragon of Loowater, UVa 11292) 你的王国里有一条n个头的恶龙,你希望雇一些骑士把它杀死(即砍掉所有头).村里有m个骑士 ...
- NET基础课--NET中程序集0-1
程序集 1.表现形式:.dll 和. exe . 2.程序集组成:PE头,CLR头,清单,元数据,CIL代码,资源文件.实际上这些内容包含在一个叫做Module的逻辑结构中. 单模块程序集:程序集就 ...
- 移动平台前端开发总结(针对iphone,Android等手机)
移动平台前端开发是指针对高端智能手机(如Iphone.Android)做站点适配也就是WebApp,并非是针对普通手机开发Wap 2.0,所以在阅读本篇文章以前,你需要对webkit内核的浏览器有一定 ...
- 《JavaScript 闯关记》之函数
函数是一段代码,它只定义一次,但可以被执行或调用任意次.在 JavaScript 里,函数即对象,程序可以随意操控它们.比如,可以把函数赋值给变量,或者作为参数传递给其他函数,也可以给它们设置属性,甚 ...
- EF MySQL 提示 Specified key was too long; max key length is 767 bytes错误
在用EF的CodeFirst操作MySql时,提示 Specified key was too long; max key length is 767 bytes错误,但数据库和表也建成功了.有高人知 ...
- OSError: [Errno 13] Permission denied: '/etc/cron.d/1sandbox_registration'
使用Hortonworks 的twitter tutorial: http://hortonworks.com/hadoop-tutorial/how-to-refine-and-visualize- ...
- hdu 4612 Warm up(无向图Tarjan+树的直径)
题意:有N个点,M条边(有重边)的无向图,这样图中会可能有桥,问加一条边后,使桥最少,求该桥树. 思路:这个标准想法很好想到,缩点后,求出图中的桥的个数,然后重建图必为树,求出树的最长直径,在该直径的 ...
- Simple Daemon Shell
PROPATH="/var/www/html/" PROGRAM="vertical" LOGNAME="/tmp/monitor.vertical. ...