第14天android:存储xml和偏好

http://www.verycd.com/topics/2915669/

15_传智播客Android视频教程_采用Pull解析器解析和生成XML内容

1.android 已经集成了pull解析器,也证明了pull的也具有优越性。

推的形式,一行一行的读,效率挺高的,但是代码写的太多了。

 

16_传智播客Android视频教程_采用SharedPreferences保存用户偏好设置参数

1.偏好参数(SharedPreferences)

利用xml存储,但是已经封装好了,用户可以查看最后的xml。


2.写入数据

    public void save() {

        String name = "wan";

        int age = 10;

        // 第一个参数:文件名称,不要后缀名.

        // 第二个参数:类型,这里设置的是私有的偏好

        SharedPreferences sp = getApplicationContext().getSharedPreferences(

                "helloOne", Context.MODE_PRIVATE);

        Editor e = sp.edit();

        e.putString("name", name);

        e.putInt("age", age);

        e.commit();// 这一步不能忘。

    }

3.xml位置

/data/data/<package_name>/shared_prefs/xxx.xml

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>

<map>

<string name="name">wan</string>

<int name="age" value="10" />

</map>

4.获取值

public String get() {

        SharedPreferences sp = getApplicationContext().getSharedPreferences(

                "helloOne", Context.MODE_PRIVATE);

        String name = sp.getString("name", "");

        int age = sp.getInt("age", 0);

        return name + ":" + age;

    }

5.另一种获取方法SharedPreferences

    public void save2(){

        //这个会得到一个默认的xml文件,就是类名

        SharedPreferences sp =this.getPreferences( Context.MODE_PRIVATE);

    }

16完

 

你可能感兴趣的:(android)