2010-11-29 android的学习 Data Storage


本文基于android SDK file进行讨论,对SDK file的细节进行讨论,来发掘android开发中的细节


 

Android Learn Map

1. User interface

2. Data Storage

...


今天学习了android中的Data storage方面的内容,虽然没有学习完成。但还是收获不少,自己的工程有可以有一些小的改动了

在android提供的数据存储方式有如下几种:

 

Shared Preferences:Store private primitive data in key-value pairs.

Internal Storage:Store private data on the device memory.

External Storage:Store public data on the shared external storage.

SQLite Databases:Store structured data in a private database.

Network Connection:Store data on the web with your own network server.

 

今天专门对Shared Perferences进行了学习,当然除了看SDK文档之外还看了下android的书籍

在运用share Perferences进行数据存储,是比较方便的,可以容易的建立存储档案。

一般使用在登录界面的数据保存和少量临时数据的存储。

 

稍微建立了一个工程文件,废话不多说,上代码。最后有解释该shared preferences的使用方法和使用地点


首先是:main.xml稍微建立了一个类似于登录界面的东西,大家熟悉的QQ啊之类软件都有这个

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:padding="10px">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leajen ShareData project"
android:textSize="25px"
/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="25px"/>
<EditText android:id="@+id/edit_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="password"
android:textSize="25px"/>
<EditText android:id="@+id/edit_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="@+id/updata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Updata"/>
<Button android:id="@+id/updata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear"/>
</LinearLayout>
</LinearLayout>

 

 

然后是sharedata.class文件

 

package Leajen.androidTest;

 

import android.app.Activity;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.widget.Button;

import android.widget.EditText;

/**

 * @author Leajen

 * @version V0.01

 * @deprecated this program function is test shared preeference

 * DataBase Parameter: PREFS_NAME

 * Key Parameter: USER_NAME, USER_PASSWORD

 * */

public class sharedata extends Activity {

    /** Called when the activity is first created. */

EditText edit_name, edit_password;

Button updata_button, clear_button;

public static final String PREFS_NAME = "LeajenData:";

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        System.out.println("System onCreate()");

 

        edit_name = (EditText)findViewById(R.id.edit_name);

        edit_password = (EditText)findViewById(R.id.edit_password);

 

        //建立shared preferences数据保存

        //定义从什么数据包获取到数据,现在是从PREFS_NAME处获取到数据

        SharedPreferences setting = getSharedPreferences(PREFS_NAME, 0);

        //从PREFS_NAME中获取到数据,如果没有数据则返回“”(空)

        edit_name.setText(setting.getString("USER_NAME", ""));

        edit_password.setText(setting.getString("USER_PASSWORD", ""));

    }

 

    //复写onstop方法,在推出该页面的时候保存数据,当然是少批量的数据

@Override

protected void onStop() {

// TODO Auto-generated method stub

super.onStop();

System.out.println("System onStop()");

edit_name = (EditText)findViewById(R.id.edit_name);

        edit_password = (EditText)findViewById(R.id.edit_password);

//在Activity停止活动的时候再一次保存数据,在下次启动的时候就可以不用再输入用户名了

SharedPreferences setting = getSharedPreferences(PREFS_NAME, 0);

//使用sharedpreference的editor方法

SharedPreferences.Editor editor = setting.edit();

//向editor中的USER_NAME,USER_PASSWORD中写入数据

editor.putString("USER_NAME", edit_name.getText().toString());

editor.putString("USER_PASSWORD", edit_password.getText().toString());

editor.commit();

}   

}

排版的话看来还需要花些功夫呢

 

 

shared preference的使用主要在一些少量数据的场合,虽然存储的数据少,但是在正式工作中对他的功能定义需要明确

在正式工程文件中,这些数据的分类的重要性某种程度上会超过代码本身

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值