admin 发表于 2017-2-20 11:14:08

Android控件使用—AutoCompleteTextView自动补全实现搜索功能

Android控件使用—AutoCompleteTextView自动补全实现搜索功能。

http://img.blog.csdn.net/20160108144805444

像这样的一个功能,首先,最上方是搜索框是一个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);
      if (history_arr.length != 0) {
            if (history_arr.length > 5) {
                String[] newArrays = new String;
                // 实现数组之间的复制
                System.arraycopy(history_arr, 0, newArrays, 0, 5);
                for (int i = 0; i < newArrays.length; i++) {
                  his_beams = new Search_Beams();
                  String str = newArrays;
                  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;
                  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);
    }

原始地址:http://blog.csdn.net/hebin320320/article/details/50482933




fisonlee 发表于 2019-3-6 08:06:05

这个帖子不错,大家快来顶起来!捡代码论坛资源就是不错。
页: [1]
查看完整版本: Android控件使用—AutoCompleteTextView自动补全实现搜索功能