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 toArray(Context context) { String history = getHistory(context); Gson gson = new Gson(); List retList = (List)gson.fromJson(history, (new TypeToken>() { }).getType()); if(retList!=null && retList.size()>9){ return retList.subList(0,9); } return retList; } public static String toJsonArray(List historyList) { Gson gson = new Gson(); return gson.toJson(historyList); } }