android 利用SharedPreferences 存储系统数据的高级运用(枚举)

android 利用SharedPreferences 存储系统数据的高级应用(枚举)
package com.lyc;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;


public class SystemAPP {
private final String PREFERENCE_NAMR = "systemdb.xml";//目标配置文件
private  SharedPreferences mySharedPreferences;
private  SharedPreferences.Editor editor;
public SystemAPP(Context context) {
mySharedPreferences = context.getSharedPreferences(PREFERENCE_NAMR, Activity.MODE_PRIVATE);
editor = mySharedPreferences.edit();
}
/**
* 根据枚举名称相对应添加或者更新新数据
* @param name 枚举名称
*/
public  boolean createOrUpdataName(MySystemEnum name){
editor.putString(name.toString(),name.getValue());
if(editor.commit()){
return true;
}
return false;
}
/**
* 根据枚举名称添加或者 修改对于的数据值
* @param name 名称
* @param value 目标值
*/
public boolean createOrUpdataName(MySystemEnum name,MySystemEnum value){
editor.putString(name.toString(), value.getValue());
if(editor.commit()){
return true;
}
return false;
}
/**
* 根据枚举名称得到相对应的值
* @param name 枚举名称
* @return  数据值
*/
public  String getValue(MySystemEnum name){
return mySharedPreferences.getString(name.getValue(), "没有数据.");
}
/**
* 情况配置文件所有内容
*/
public boolean deleteAll(){
editor.clear();
if(editor.commit()){
return true;
}
return false;
}
/**
* 根据枚举名称来删除该数据
* @param name 枚举名称
*/
public boolean deleteName(MySystemEnum name){
editor.remove(name.toString());
if(editor.commit()){
return true;
}
return false;
}
 

}


package com.lyc;
/*
 * 系统配置文件枚举
 */
public enum MySystemEnum {
homepage("homepage"),   //首页
newProduct("newProduct"),   //新品
cookRecommend("cookRecommend"),   //厨师推荐
foodLevel("foodLevel"),  //菜单级别
news("news"),   //公告栏
video("video"),   //视频
food1("food"),  //菜

foodName("foodName"),
foodValue1("foodType1"),
foodValue2("foodType2");

private String value;
MySystemEnum(String value){
this.value = value;
}
public String getValue() {
return value;
}
}


package com.lyc;


import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class SystemDBActivity extends Activity {
    /** Called when the activity is first created. */
private TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SystemAPP app = new SystemAPP(this);
        app.createOrUpdataName(MySystemEnum.foodLevel);
        textView = (TextView)findViewById(R.id.text_name);
        textView.setText(app.getValue(MySystemEnum.foodLevel));
        app.createOrUpdataName(MySystemEnum.foodLevel,MySystemEnum.foodName);
    }
}