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 ...
随机推荐
- sqlite数据库读写在linux下的权限问题
近期在学linux,恰巧有个php项目要做.于是配置好环境打算在linux下做. 无奈站点执行后一片空白.经过调试发现是sqlite数据库的问题. 安装sqlite扩展 apt-get install ...
- Hacker(五)----黑客专用通道--->端口
计算机中,端口是计算机与外部进行通信交流的出口.计算机本身携带的物理端口(键盘.鼠标.显示器等输入/输出接口)已经无法满足网络通信的要求,因此TCP/IP协议就引入了一种称为Socket的应用程序接口 ...
- Sublime Text插件FileHeader实践
FileHeader是一个文件模板插件,可以定制各种文件模板和文件头部信息,保存时可以自动更新文件的修改时间.在多人开发中这个功能相当实用. 具体介绍我就不细说了,主要是分享一下在使用FileHead ...
- oc特有语法
分类 问题 1.什么是分类? 就是把一个类的功能,分出一部分来放在一个独立的文件中 2.分类的语法是什么样的? @interface Person(SuperMan) 3.分类与类是什么关系? 分类依 ...
- 天天模拟器极速畅玩靠谱游戏《仙境传说RO:复兴》
在电脑上用模拟器打开手游<仙境传说RO:复兴>,今天小编就来写一写天天模拟器的试玩教学. 首先先打开天天模拟器极速版. 在界面中找到鱼图标的靠谱游戏应用中心. 在应用中心中找到<仙境 ...
- 设定MS SQL Server 2008定期自动备份
1.说明 SQL Server2008 本身具有定期自动备份功能,我们只需要通过简单的配置就可以实现非常简单高效的自动备份功能. 2.打开SQL Server代理服务 要实现自动备份功能,首先要保证S ...
- [转载]浅析Windows安全相关的一些概念
Session 我们平常所说的Session是指一次终端登录, 这里的终端登录是指要有自己的显示器和鼠标键盘等, 它包括本地登录和远程登录.在XP时代每次终端登录才会创建一个Session,但是在Vi ...
- Intent七大属性之总结
参考<疯狂android讲义>第5章 1.Intent 用于封装程序的"调用意图",不管想启动一个Acitivity.Service还是BroadcastReceive ...
- 现代OpenGL教程 01 - 入门指南
原文链接传送门 译序 早前学OpenGL的时候还是1.x版本,用的都是glVertex,glNormal等固定管线API.后来工作需要接触DirectX9,shader也只是可选项而已,跟固定管线一起 ...
- Django学习(一) Django安装配置
上一节介绍了如何搭建Python的开发环境,这次介绍一下如何搭建Django的开发环境. 第一.下载Django Django跟Python的版本对应 Django version Python ve ...