美文网首页
Android实现搜索历史记录的本地存储。

Android实现搜索历史记录的本地存储。

作者: 背锅TV丶伴奏大师 | 来源:发表于2018-09-26 11:17 被阅读502次

    主要通过SharedPreferences进行存储,难点主要是1.倒序显示 2.去重 3.最多显示n条。话不多说看代码

    private final static String PREFERENCE_NAME = "superservice_ly";
    private final static String SEARCH_HISTORY="linya_history";
    // 保存搜索记录
        public static void saveSearchHistory(String inputText) {
            SharedPreferences sp = App.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            if (TextUtils.isEmpty(inputText)) {
                return;
            }
            String longHistory = sp.getString(SEARCH_HISTORY, "");  //获取之前保存的历史记录
            String[] tmpHistory = longHistory.split(","); //逗号截取 保存在数组中
            List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory)); //将改数组转换成ArrayList
            SharedPreferences.Editor editor = sp.edit();
            if (historyList.size() > 0) {
                //1.移除之前重复添加的元素
                for (int i = 0; i < historyList.size(); i++) {
                    if (inputText.equals(historyList.get(i))) {
                        historyList.remove(i);
                        break;
                    }
                }
                historyList.add(0, inputText); //将新输入的文字添加集合的第0位也就是最前面(2.倒序)
                if (historyList.size() > 8) {
                    historyList.remove(historyList.size() - 1); //3.最多保存8条搜索记录 删除最早搜索的那一项
                }
                //逗号拼接
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < historyList.size(); i++) {
                    sb.append(historyList.get(i) + ",");
                }
                //保存到sp
                editor.putString(SEARCH_HISTORY, sb.toString());
                editor.commit();
            } else {
                //之前未添加过
                editor.putString(SEARCH_HISTORY, inputText + ",");
                editor.commit();
            }
        }
    //获取搜索记录
        public static List<String> getSearchHistory(){
            SharedPreferences sp = App.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
            String longHistory =sp.getString(SEARCH_HISTORY, "");
            String[] tmpHistory = longHistory.split(","); //split后长度为1有一个空串对象
            List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory));
            if (historyList.size() == 1 && historyList.get(0).equals("")) { //如果没有搜索记录,split之后第0位是个空串的情况下
                historyList.clear();  //清空集合,这个很关键
            }
            return historyList;
        }
    

    代码就这些,放到你的工具类里面,在你需要的地方调用就好了。

    相关文章

      网友评论

          本文标题:Android实现搜索历史记录的本地存储。

          本文链接:https://www.haomeiwen.com/subject/aejdoftx.html