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);
    }
發佈了61 篇原創文章 · 獲贊 72 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章