免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2188 | 回复: 1
打印 上一主题 下一主题

通过改写ArrayAdapter 用AutoCompleteTextView 实现汉字和拼音双向关联 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-04 14:51 |只看该作者 |倒序浏览
通过改写ArrayAdapter 用AutoCompleteTextView 实现汉字和拼音双向关联











package com.bn.lccx;
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.List;
  6. import android.content.Context;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.BaseAdapter;
  12. import android.widget.Filter;
  13. import android.widget.Filterable;
  14. import android.widget.TextView;
  15. public class CityAdapter<T> extends BaseAdapter implements Filterable
  16. {
  17.     private List<T> mObjects;//城市名称  汉字数组
  18.     private List<T> mObjects2;//城市名称  拼音数组
  19.     private final Object mLock = new Object();
  20.     private int mResource;//展示数组适配器内容的View Id
  21.     private int mDropDownResource;//下拉框中内容的Id
  22.     private int mFieldId = 0;//下拉框选项ID
  23.     private boolean mNotifyOnChange = true;
  24.     private Context mContext;//当前上下文对象 - Activity
  25.     private ArrayList<T> mOriginalValues;//原始数组列表
  26.     private ArrayFilter mFilter;//
  27.     private LayoutInflater mInflater;
  28. public CityAdapter(Context context, int textViewResourceId, T[] objects,T[] objects2)
  29. {
  30.     init(context, textViewResourceId, 0, Arrays.asList(objects),Arrays.asList(objects2));
  31. }
  32. public void add(T object)
  33. {
  34.     if (mOriginalValues != null)
  35.     {
  36.         synchronized (mLock)
  37.         {
  38.             mOriginalValues.add(object);
  39.             if (mNotifyOnChange) notifyDataSetChanged();
  40.         }
  41.     }
  42.     else
  43.     {
  44.         mObjects.add(object);
  45.         if (mNotifyOnChange) notifyDataSetChanged();
  46.     }
  47. }
  48. public void insert(T object, int index)
  49. {
  50.     if (mOriginalValues != null)
  51.     {
  52.         synchronized (mLock)
  53.         {
  54.             mOriginalValues.add(index, object);
  55.             if (mNotifyOnChange) notifyDataSetChanged();
  56.         }
  57.     }
  58.     else
  59.     {
  60.         mObjects.add(index, object);
  61.         if (mNotifyOnChange) notifyDataSetChanged();
  62.     }
  63. }
  64. public void remove(T object)
  65. {
  66.     if (mOriginalValues != null)
  67.     {
  68.         synchronized (mLock)
  69.         {
  70.             mOriginalValues.remove(object);
  71.         }
  72.     }
  73.     else
  74.     {
  75.         mObjects.remove(object);
  76.     }
  77.     if (mNotifyOnChange) notifyDataSetChanged();
  78. }
  79. public void clear() //从列表中删除所有的信息
  80. {
  81.     if (mOriginalValues != null)
  82.     {
  83.         synchronized (mLock)
  84.         {
  85.             mOriginalValues.clear();
  86.         }
  87.     }
  88.     else
  89.     {
  90.         mObjects.clear();
  91.     }
  92.     if (mNotifyOnChange) notifyDataSetChanged();
  93. }
  94. public void sort(Comparator<? super T> comparator)//根据指定的比较器对适配器中的内容进行排序
  95. {
  96.     Collections.sort(mObjects, comparator);
  97.     if (mNotifyOnChange) notifyDataSetChanged();        
  98. }
  99. @Override
  100. public void notifyDataSetChanged()
  101. {
  102.     super.notifyDataSetChanged();
  103.     mNotifyOnChange = true;
  104. }
  105. //设置自动修改
  106. public void setNotifyOnChange(boolean notifyOnChange)
  107. {
  108.     mNotifyOnChange = notifyOnChange;
  109. }
  110. //构造器  -- 初始化所有信息
  111. private void init(Context context, int resource, int textViewResourceId, List<T> objects ,List<T> objects2)
  112. {
  113.     mContext = context;
  114.     mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  115.     mResource = mDropDownResource = resource;
  116.     mObjects = objects;
  117.     mObjects2 = objects2;
  118.     mFieldId = textViewResourceId;
  119. }
  120. //返回数组适配器相关联的上下文对象
  121. public Context getContext()
  122. {
  123.     return mContext;
  124. }
  125. public int getCount() //返回  城市名称汉字  列表的大小
  126. {
  127.     return mObjects.size();
  128. }

  129. public T getItem(int position)//返回城市名称汉字列表中指定位置的字符串的值
  130. {
  131.     return mObjects.get(position);
  132. }
  133. public int getPosition(T item)//返回 城市名称汉字 列表中指定的 字符串值 的索引
  134. {
  135.     return mObjects.indexOf(item);
  136. }
  137. public long getItemId(int position)//将int型整数以长整型返回
  138. {
  139.     return position;
  140. }
  141. public View getView(int position, View convertView, ViewGroup parent)//创建View
  142. {
  143.     return createViewFromResource(position, convertView, parent, mResource);
  144. }
  145. private View createViewFromResource(int position, View convertView, ViewGroup parent,//创建View
  146.         int resource)
  147. {
  148.     View view;
  149.     TextView text;
  150.     if (convertView == null) //如果当前为空
  151.     {
  152.         view = mInflater.inflate(resource, parent, false);
  153.     }
  154.     else //如果不为空
  155.     {
  156.         view = convertView;
  157.     }

  158.     try {
  159.         if (mFieldId == 0) //如果当前域为空,假定所有的资源就是一个TextView
  160.         {
  161.             text = (TextView) view;
  162.         }
  163.         else//否则,在界面中找到TextView
  164.         {
  165.             text = (TextView) view.findViewById(mFieldId);
  166.         }
  167.     }
  168.     catch (ClassCastException e) //异常处理
  169.     {
  170.         throw new IllegalStateException
  171.         (
  172.            "ArrayAdapter requires the resource ID to be a TextView", e
  173.         );
  174.     }
  175.     text.setText(getItem(position).toString());//为Text设值  -返回当前城市名称汉字列表中选中的值
  176.     return view;
  177. }
  178. public void setDropDownViewResource(int resource) //创建下拉视图
  179. {
  180.     this.mDropDownResource = resource;
  181. }
  182. @Override
  183. public View getDropDownView(int position, View convertView, ViewGroup parent)
  184. {
  185.     return createViewFromResource(position, convertView, parent, mDropDownResource);
  186. }
  187. public static ArrayAdapter<CharSequence> createFromResource(Context context,//从外部资源中创建新的数组适配器
  188.         int textArrayResId, int textViewResId)
  189. {
  190.     CharSequence[] strings = context.getResources().getTextArray(textArrayResId);//创建字符创序列
  191.     return new ArrayAdapter<CharSequence>(context, textViewResId, strings);//返回数组适配器
  192. }
  193. public Filter getFilter() //得到过滤器
  194. {
  195.     if (mFilter == null)//如果为空,创建数组过滤器
  196.     {
  197.         mFilter = new ArrayFilter();
  198.     }
  199.     return mFilter;
  200. }
  201. //数组过滤器限制数组适配器以指定的前缀开头,如果跟提供的前缀不匹配,则将其从中删除
  202. private class ArrayFilter extends Filter
  203. {
  204.     @Override
  205.     protected FilterResults performFiltering(CharSequence prefix)//执行过滤
  206.     {
  207.         FilterResults results = new FilterResults();//创建FilterResults对象
  208.         if (mOriginalValues == null) //如果为空
  209.         {
  210.             synchronized (mLock)
  211.             {
  212.                 mOriginalValues = new ArrayList<T>(mObjects);
  213.             }
  214.         }
  215.         if (prefix == null || prefix.length() == 0)
  216.         {
  217.             synchronized (mLock)
  218.             {
  219.                 ArrayList<T> list = new ArrayList<T>(mOriginalValues);
  220.                 results.values = list;
  221.                 results.count = list.size();
  222.             }
  223.         }
  224.         else
  225.         {
  226.             String prefixString = prefix.toString().toLowerCase();//转换成小写
  227.             final ArrayList<T> values = mOriginalValues;
  228.             final int count = values.size();
  229.             final ArrayList<T> newValues = new ArrayList<T>(count);
  230.             for (int i = 0; i < count; i++)
  231.             {
  232.                 final T value = values.get(i);
  233.                 final String valueText = value.toString().toLowerCase();

  234.                 final T value2 = mObjects2.get(i);
  235.                 final String valueText2 = value2.toString().toLowerCase();
  236.                
  237.                 //查找拼音
  238.                 if(valueText2.startsWith(prefixString))
  239.                 {
  240.                         newValues.add(value);
  241.                 }//查找汉字      
  242.                 else if(valueText.startsWith(prefixString))
  243.                 {
  244.                         newValues.add(value);
  245.                 }
  246.                 else
  247.                 {      //添加汉字关联
  248.                         final String[] words = valueText.split(" ");
  249.                         final int wordCount = words.length;
  250.                         for (int k = 0; k < wordCount; k++)
  251.                         {
  252.                             if (words[k].startsWith(prefixString))
  253.                             {
  254.                                 newValues.add(value);
  255.                                 break;
  256.                             }
  257.                         }
  258.                         //添加拼音关联汉字
  259.                         final String[] words2 = valueText2.split(" ");
  260.                         final int wordCount2 = words2.length;

  261.                     for (int k = 0; k < wordCount2; k++)
  262.                     {
  263.                         if (words2[k].startsWith(prefixString))
  264.                         {
  265.                             newValues.add(value);
  266.                             break;
  267.                         }
  268.                     }  
  269.                 }
  270.             }
  271.             results.values = newValues;
  272.             results.count = newValues.size();
  273.         }
  274.         return results;
  275.     }
  276.     @SuppressWarnings("unchecked")
  277.     protected void publishResults(CharSequence constraint, FilterResults results)
  278.     {   
  279.         mObjects = (List<T>) results.values;
  280.         if (results.count > 0)
  281.         {
  282.             notifyDataSetChanged();
  283.         } else
  284.         {
  285.             notifyDataSetInvalidated();
  286.         }
  287.     }
  288. }
  289. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-04 14:51 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP