android中的三种数据持久化方式:
文件存储、SharedPreference存储、数据库存储
- 文件储存
借用context类提供的openFileOutput方法,第一个参数即文件名,第二中为存储方式MODE_PRIVTAE:同名覆盖,MODE_APPEND:同名不覆盖,后面追加.
布局文件:
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
存数据:
@Override
protected void onDestroy() {
super.onDestroy();
String data = editText.getText().toString();
try {
save(data);
} catch (IOException e) {
e.printStackTrace();
}
}
//存数据
private void save(String data) throws IOException {
FileOutputStream fileOutputStream = null;
BufferedWriter writer = null;
try {
fileOutputStream = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
writer.write(data);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (writer != null)
writer.close();
}
}
取数据
//取数据
private String load() {
FileInputStream fileInStream = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();
try {
fileInStream = openFileInput("data");
reader = new BufferedReader(new InputStreamReader(fileInStream));
String line="";
if ((line = reader.readLine()) != null){
content.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content.toString();
}
String text = load();
editText.setText(text);
editText.setSelection(text.length());//光标停留在文字的结尾处
- SharedPreference存储
首先获取到SharedPreference对象。三种方式:
- Context类中的getSharedPreferences()方法,两参数:第一个指定文件名称,第二个指定操作模式
- Activity类中的SharedPreferences()方法,与上面几乎一样,只有后面一个参数
- PreferenceManager类中的SharedPreferences()方法
布局文件
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
存储数据
SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("name","tom");//键值对的形式存储数据
editor.putInt("age",23);
editor.putBoolean("married",false);
editor.apply();
数据库存储
觉得原生的太麻烦,直接使用后LitePal的来练习
添加依赖
compile 'org.litepal.android:core:1.4.1'
配置litepal.xml
<?xml version="1.0" encoding="utf-8" ?>
<litepal>
<dbname value = "BookStore"></dbname>
<version value="1"></version>
<list>
</list>
</litepal>
配置LitepalApplication
<application
android:name="org.litepal.LitePalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
创建bean
package com.example.bbw.litepaldemo;
/**
* Created by bbw on 2017/7/21.
* bean
*/
public class Book {
private int id;
private String author;
private double price;
private int pages;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
数据库中的表(映射产生):
<list>
<mapping class="com.example.bbw.litepaldemo.Book"/>
</list>
创建数据库
public void onClick(View view) {
LitePal.getDatabase();//创建数据库
}