Android简便的使用SharedPreference做存储之smartsharedpreferences

在安卓项目中少不了使用SharedPreference来做数据存储,但是一般的使用都需要自己定义一些方法和管理key,今天记录一下使用smartsharedpreferences来简便的做SharedPreference存储。

一般的SharedPreference至少需要我们来获取Editor,存储的时候需要我们来put数据,再apply:

private static SharedPreferences getPuhuiSP() {
    SharedPreferences mSP = LKApp.getAppContext().getSharedPreferences(“user_info”, Context.MODE_PRIVATE);
    return mSP;
}

private static SharedPreferences.Editor getEditor() {
    return getPuhuiSP().edit();
}

/**
 * 根据key删除保存的值
 *
 * @param key
 */
public static void remove(String key) {
    SharedPreferences.Editor editor = getEditor();
    editor.remove(key);
    editor.apply();
}

/**
 * int
 *
 * @param key
 * @param value
 */
public static void putInt(String key, int value) {
    SharedPreferences.Editor editor = getEditor();
    editor.putInt(key, value);
    editor.apply();
}

public static int getInt(String key, int defaultValue) {
    return getPuhuiSP().getInt(key, defaultValue);
}

如果你不想让你的key混乱,你还需要管理你的key:

// phone number
public static final String PHONE = "phone";
// id number
public static final String IDNUM = "id_num";
// name
public static final String IDNAME = "name";
// pwd
public static final String PWD = "pwd";

今天发现了一个简便的SP存储的库:smartsharedpreferences

implementation 'me.yokeyword.smartsharedpreferences:api:1.0.0'
annotationProcessor 'me.yokeyword.smartsharedpreferences:compiler:1.0.0'

使用非常轻便:
1、首先你存储的数据可以分类管理,可以把一类的数据放在一起,比如用户数据,创建一个bean类:

package com.txhl.testapp.bean;

import me.yokeyword.api.Spf;

/**
 * Created by chen.yingjie on 2018/12/20.
 */
@Spf
public class User {
    private String id;

    private String name;

    private String mobile;

    private String first;
}

2、Rebuild Project生成bean对应的Helper,(注意:是编译生成)
在这里插入图片描述
3、使用很便捷,put()的参数类型根据bean类来的

private Spf_User spf_user;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rx);
    spf_user = Spf_User.create(this);
}

private void save() {
        String etName = et_name.getText().toString();
        String etId = et_id.getText().toString();
        String etMobile = et_mobile.getText().toString();
        String etFirst = et_first.getText().toString();
        boolean commit = spf_user.edit()
                .name().put(etName)
                .mobile().put(etMobile)
                .first().put(etFirst)
                .id().put(etId)
                .commit();
//                .apply();// 提交保存方法,commit返回Boolean,apply无返回值
        if (commit) {
            ToastUtil.showShort(this, "保存成功");
        } else {
            ToastUtil.showShort(this, "请输入数据");
        }
    }

    private void get() {
        String name = spf_user.name().get();
        String id = spf_user.id().get();
        String mobile = spf_user.mobile().get();
        String first = spf_user.first().get();
        tv_name.setText("id:" + id + ", name:" + name + ", mobile:" + mobile + ", first:" + first);
    }

我们来看看怎么实现的
首先看Spf_User.java

package com.txhl.testapp.bean;

import android.content.Context;
import android.content.SharedPreferences.Editor;
import me.yokeyword.api.spf.EditorHelper;
import me.yokeyword.api.spf.SpfHelper;
import me.yokeyword.api.spf.field.StringEditorField;
import me.yokeyword.api.spf.field.StringSpfField;

public final class Spf_User extends SpfHelper {
  private Spf_User(Context context) {
    super(context, "_User");
  }

  public static Spf_User create(Context context) {
    return new Spf_User(context);
  }

  public StringSpfField id() {
    return new StringSpfField(sharedPreferences,"id");
  }

  public StringSpfField name() {
    return new StringSpfField(sharedPreferences,"name");
  }

  public StringSpfField mobile() {
    return new StringSpfField(sharedPreferences,"mobile");
  }

  public StringSpfField first() {
    return new StringSpfField(sharedPreferences,"first");
  }

  public Editor_User edit() {
    return new Editor_User(getEditor());
  }

  public final class Editor_User extends EditorHelper {
    public Editor_User(Editor editor) {
      super(editor);
    }

    public StringEditorField<Editor_User> id() {
      return new StringEditorField(this,"id");
    }

    public StringEditorField<Editor_User> name() {
      return new StringEditorField(this,"name");
    }

    public StringEditorField<Editor_User> mobile() {
      return new StringEditorField(this,"mobile");
    }

    public StringEditorField<Editor_User> first() {
      return new StringEditorField(this,"first");
    }
  }
}

Spf_User.create(this);
点进去,走到Spf_User的构造器的super(context, “_User”);

public abstract class SpfHelper {

    protected final SharedPreferences sharedPreferences;

    public SpfHelper(Context context, String suffixName) {
        this.sharedPreferences = context.getSharedPreferences(getSpfName() + suffixName, 0);
    }

    private String getSpfName() {
        return "MySpf";
    }

    public final SharedPreferences.Editor getEditor() {
        return sharedPreferences.edit();
    }

    public final void clear() {
        sharedPreferences.edit().clear().apply();
    }
}

这里面把所有的准备工作都做好了,也就是说你创建的bean类是作为SP存储的文件名了,完整文件名为:MySpf_Bean只需要往里put数据就行了,因为在Spf_User.java里以你的bean类的属性名作为key为你存储数据。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值