AutoCompleteTextView自动补全实现搜索功能

这里写图片描述

像这样的一个功能,首先,最上方是搜索框是一个AutoCompleteTextView。
搜索的时候,将搜索的文字存到本地文件,然后再将本地文件存的值取出来加载成列表,也就是历史搜索记录:
将搜索文字写入本地文件的代码,这里将每个搜索的文字都存到本地,如果本地存在搜索的文字,那么就移除本地那个存在的文字,再将搜索的文字放入文件的最后一个,也就是历史搜索记录的最上边:

public void writeHistory(String path) {
        LostFocus.HideInputText(context, title_search_tv_search);
        Context ctx = this;
        SharedPreferences SAVE = ctx.getSharedPreferences("save", MODE_PRIVATE);
        List<String> date = readHistory();
        if (!date.contains(path)) {
            int n = SAVE.getInt("point", 0);
            SharedPreferences.Editor editor = SAVE.edit();
            editor.putString("path" + n, path);
            editor.putInt("point", (n + 1) % 16);
            editor.commit();
        }else {
            int k = SAVE.getInt("point",0);
            date.remove(path);
            date.add(k-1, path);
            cleanHistory();
            for (String str : date) {
                int n = SAVE.getInt("point", 0);
                SharedPreferences.Editor editor = SAVE.edit();
                editor.putString("path" + n, str);
                editor.putInt("point", (n + 1) % 16);
                editor.commit();
            }
        }

    }

将存的本地文件读取出来,返回一个list:

 public List<String> readHistory() {
        List<String> list = new ArrayList<String>();
        Context ctx = this;
        SharedPreferences SAVE = ctx.getSharedPreferences("save", MODE_PRIVATE);
        int point = SAVE.getInt("point", 0);
        String path;
        final int N = 16;
        for (int i = 0, n = point; i <= N; i++) {
            path = SAVE.getString("path" + n, null);
            if (path != null) {
                list.add(path);
            }
            n = n > 0 ? (--n) : (--n + N) % 16;
        }
        return list;
    }

清空历史记录:

public void cleanHistory() {
        SharedPreferences sp = getSharedPreferences("save", 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
        super.onDestroy();
    }

历史记录列表

    private void History_list() {
        list1.clear();
        List<String> date = readHistory();
        String[] history_arr = date.toArray(new String[date.size()]);
        if (history_arr.length != 0) {
            if (history_arr.length > 5) {
                String[] newArrays = new String[5];
                // 实现数组之间的复制
                System.arraycopy(history_arr, 0, newArrays, 0, 5);
                for (int i = 0; i < newArrays.length; i++) {
                    his_beams = new Search_Beams();
                    String str = newArrays[i];
                    his_beams.setHistoryTitle(str);
                    list1.add(his_beams);
                }
            } else {
                for (int i = 0; i < history_arr.length; i++) {
                    his_beams = new Search_Beams();
                    String str = history_arr[i];
                    his_beams.setHistoryTitle(str);
                    list1.add(his_beams);
                }
            }
            history_adapter = new History_list(Search.this, context, list1);
            main_search_history_listview.setAdapter(history_adapter);
        } 
    }

读取本地文件,将搜索记录自动匹配展示出来:

 private void ReadAuto() {
        List<String> date = readHistory();
        arr_adapter = new ArrayAdapter<>(context, R.layout.main_search_auto_item_01, date);
        main_search_title_actv.setAdapter(arr_adapter);
        main_search_title_actv.setDropDownHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        main_search_title_actv.setThreshold(1);
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值