pda/zhuike/.svn/pristine/51/5174ae58f7ba2fc32337094fa95...

49 lines
1.5 KiB
Plaintext
Raw Normal View History

2024-02-06 22:23:29 +08:00
package com.novelbook.android.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;
public class HistoryCache {
public HistoryCache() {
}
public static void clear(Context context) {
SharedPreferences sp = context.getSharedPreferences("record", 0);
sp.edit().clear().commit();
}
public static void saveHistory(Context context, String result) {
SharedPreferences sp = context.getSharedPreferences("record", 0);
Editor editor = sp.edit();
editor.putString("record_key", result);
editor.commit();
}
public static String getHistory(Context context) {
SharedPreferences sp = context.getSharedPreferences("record", 0);
String result = sp.getString("record_key", (String)null);
return result;
}
public static List<String> toArray(Context context) {
String history = getHistory(context);
Gson gson = new Gson();
List<String> retList = (List)gson.fromJson(history, (new TypeToken<List<String>>() {
}).getType());
if(retList!=null && retList.size()>9){
return retList.subList(0,9);
}
return retList;
}
public static String toJsonArray(List<String> historyList) {
Gson gson = new Gson();
return gson.toJson(historyList);
}
}