工具类: 历史记录工具类 HistoryUtils
- 历史记录工具类 例如:搜索历史记录的保存,更新,清除单条,清除全部
public class HistoryUtils {
public static List<String> getHistoryList(Context context, String hisKey) {
String communitySearchHistory = SPUtils.getString(context, hisKey);
List<String> historyList = new Gson().fromJson(communitySearchHistory, new TypeToken<List<String>>() {
}.getType());
return historyList;
}
public static void addOrUpdateHis(Context context, List<String> historyList, String hisKey, String currentString) {
Iterator<String> it = historyList.iterator();
while (it.hasNext()) {
String currentHistory = it.next();
if (currentString.equals(currentHistory)) {
it.remove();
}
}
historyList.add(0, currentString);
SPUtils.put(context, hisKey, new Gson().toJson(historyList));
}
public static void removeSingleHistory(Context context, List<String> historyList, String hisKey, int position) {
historyList.remove(position);
SPUtils.put(context, hisKey, new Gson().toJson(historyList));
}
public static void removeAllHistory(Context context, List<String> historyList, String hisKey) {
historyList.clear();
SPUtils.remove(context, hisKey);
}
}