Commit 56ccf574 by patpat

增加本地存储类

parent 77586532
package com.bgycc.smartcanteen.Storage;
import android.content.Context;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PayStorage extends Storage {
static final String TAG = PayStorage.class.getSimpleName();
enum Key {
PAY_LIST
}
static PayStorage sInstance;
public static void initialize(Context context) {
sInstance = new PayStorage(context);
}
public static PayStorage getInstance() {
return sInstance;
}
JSONArray mPayList = new JSONArray();
PayStorage(Context context) {
super(TAG, context);
try {
String tmp = mSharedPreferences.getString(Key.PAY_LIST.name(), "[]");
mPayList = new JSONArray(tmp);
} catch (JSONException e) {
e.printStackTrace();
}
}
public synchronized void add(JSONObject payInfo) {
mPayList.put(payInfo);
}
public synchronized JSONArray getPayList() {
return mPayList;
}
public synchronized void savePayList() {
mSharedPreferences.edit()
.putString(Key.PAY_LIST.name(), mPayList.toString())
.apply();
}
public synchronized void clearPayList() {
mPayList = new JSONArray();
mSharedPreferences.edit()
.remove(Key.PAY_LIST.name())
.apply();
}
}
package com.bgycc.smartcanteen.Storage;
import android.content.Context;
import android.content.SharedPreferences;
public abstract class Storage {
SharedPreferences mSharedPreferences;
protected Storage(String name, Context context) {
mSharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment