阿里云服务器免费领卷啦。

捡代码论坛-最全的游戏源码下载技术网站!

 找回密码
 立 即 注 册

QQ登录

只需一步,快速开始

搜索
关于源码区的附件失效或欺骗帖, 处理办法
查看: 2231|回复: 1

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

[复制链接]

4208

主题

210

回帖

12万

积分

管理员

管理员

Rank: 9Rank: 9Rank: 9

积分
126159
QQ
发表于 2017-2-20 11:14:08 | 显示全部楼层 |阅读模式
Android控件使用—AutoCompleteTextView自动补全实现搜索功能。



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

  1. public void writeHistory(String path) {
  2.         LostFocus.HideInputText(context, title_search_tv_search);
  3.         Context ctx = this;
  4.         SharedPreferences SAVE = ctx.getSharedPreferences("save", MODE_PRIVATE);
  5.         List<String> date = readHistory();
  6.         if (!date.contains(path)) {
  7.             int n = SAVE.getInt("point", 0);
  8.             SharedPreferences.Editor editor = SAVE.edit();
  9.             editor.putString("path" + n, path);
  10.             editor.putInt("point", (n + 1) % 16);
  11.             editor.commit();
  12.         }else {
  13.             int k = SAVE.getInt("point",0);
  14.             date.remove(path);
  15.             date.add(k-1, path);
  16.             cleanHistory();
  17.             for (String str : date) {
  18.                 int n = SAVE.getInt("point", 0);
  19.                 SharedPreferences.Editor editor = SAVE.edit();
  20.                 editor.putString("path" + n, str);
  21.                 editor.putInt("point", (n + 1) % 16);
  22.                 editor.commit();
  23.             }
  24.         }

  25.     }
复制代码
将存的本地文件读取出来,返回一个list:
  1. public List<String> readHistory() {
  2.         List<String> list = new ArrayList<String>();
  3.         Context ctx = this;
  4.         SharedPreferences SAVE = ctx.getSharedPreferences("save", MODE_PRIVATE);
  5.         int point = SAVE.getInt("point", 0);
  6.         String path;
  7.         final int N = 16;
  8.         for (int i = 0, n = point; i <= N; i++) {
  9.             path = SAVE.getString("path" + n, null);
  10.             if (path != null) {
  11.                 list.add(path);
  12.             }
  13.             n = n > 0 ? (--n) : (--n + N) % 16;
  14.         }
  15.         return list;
  16.     }
复制代码
清空历史记录:
  1. public void cleanHistory() {
  2.         SharedPreferences sp = getSharedPreferences("save", 0);
  3.         SharedPreferences.Editor editor = sp.edit();
  4.         editor.clear();
  5.         editor.commit();
  6.         super.onDestroy();
  7.     }
复制代码

  1.     private void History_list() {
  2.         list1.clear();
  3.         List<String> date = readHistory();
  4.         String[] history_arr = date.toArray(new String[date.size()]);
  5.         if (history_arr.length != 0) {
  6.             if (history_arr.length > 5) {
  7.                 String[] newArrays = new String[5];
  8.                 // 实现数组之间的复制
  9.                 System.arraycopy(history_arr, 0, newArrays, 0, 5);
  10.                 for (int i = 0; i < newArrays.length; i++) {
  11.                     his_beams = new Search_Beams();
  12.                     String str = newArrays[i];
  13.                     his_beams.setHistoryTitle(str);
  14.                     list1.add(his_beams);
  15.                 }
  16.             } else {
  17.                 for (int i = 0; i < history_arr.length; i++) {
  18.                     his_beams = new Search_Beams();
  19.                     String str = history_arr[i];
  20.                     his_beams.setHistoryTitle(str);
  21.                     list1.add(his_beams);
  22.                 }
  23.             }
  24.             history_adapter = new History_list(Search.this, context, list1);
  25.             main_search_history_listview.setAdapter(history_adapter);
  26.         }
  27.     }
复制代码
读取本地文件,将搜索记录自动匹配展示出来:
  1. private void ReadAuto() {
  2.         List<String> date = readHistory();
  3.         arr_adapter = new ArrayAdapter<>(context, R.layout.main_search_auto_item_01, date);
  4.         main_search_title_actv.setAdapter(arr_adapter);
  5.         main_search_title_actv.setDropDownHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
  6.         main_search_title_actv.setThreshold(1);
  7.     }
复制代码

  1. 原始地址:http://blog.csdn.net/hebin320320/article/details/50482933
复制代码






捡代码论坛-最全的游戏源码下载技术网站! - 论坛版权郑重声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与捡代码论坛-最全的游戏源码下载技术网站!享有帖子相关版权
3、捡代码论坛版权,详细了解请点击。
4、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
5、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。 我们不承担任何技术及版权问题,且不对任何资源负法律责任。
6、如无法链接失效或侵犯版权,请给我们来信:jiandaima@foxmail.com

回复

使用道具 举报

0

主题

161

回帖

992

积分

高级会员

Rank: 4

积分
992
发表于 2019-3-6 08:06:05 | 显示全部楼层
登录可见评论
回复

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立 即 注 册

本版积分规则

技术支持
在线咨询
QQ咨询
3351529868

QQ|手机版|小黑屋|捡代码论坛-专业源码分享下载 ( 陕ICP备15015195号-1|网站地图

GMT+8, 2024-4-23 14:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表