- 浏览: 236518 次
- 性别:
- 来自: 内蒙古
文章分类
- 全部博客 (236)
- Android 功能实现 (31)
- sql数据库的学习 (8)
- Android 美化界面 (2)
- Android 优化 (1)
- Ruby on Rails 方面 (45)
- git 方面的学习 (1)
- ruby 编程的琢磨 (13)
- linux下工具软件 (13)
- 操作系统的学习 (40)
- 非技术 (13)
- 网站开发 (18)
- js 学习笔记 (19)
- css学习 (5)
- 回顾总结 (2)
- Delphi 学习 (2)
- C 语言学习笔记 (1)
- 数据结构 (1)
- node js 学习 (6)
- 设计模式 (2)
- mongdb 学习笔记 (0)
- 软件服务 (3)
- osx系统 (4)
- 搜索引擎 (1)
- 测试工具 (1)
- Aliyun (1)
- 前端JS (1)
- python学习 (0)
- iOS系统 (1)
- 分布式锁 (1)
- 开发工具 (0)
- java代码 (2)
- java (0)
最新评论
-
jiguanghover:
写的不错,收藏一下
Ubuntu下RVM, Ruby, rails安装详细 和 卸载 -
maoghj:
回顾总结(二) -
yun2223:
对楼主表示感谢
Android控件开发之Gallery3D效果 -
zw_lovec:
说清楚点吧 亲 加点注释
out of memory -
lzyfn123:
http://www.iteye.com/images/smi ...
ruby-string 字符串的学习
- public class CityAdapter<T> extends BaseAdapter implements Filterable {
- /**
- * Contains the list of objects that represent the data of this ArrayAdapter
.
- * The content of this list is referred to as "the array" in the documentation.
- */
- private List<T> mObjects;
- private List<T> mObjects2;
-
- /**
- * Lock used to modify the content of {@link #mObjects}. Any write operation
- * performed on the array should be synchronized on this lock. This lock is also
- * used by the filter (see {@link #getFilter()} to make a synchronized copy of
- * the original array of data.
- */
- private final Object mLock = new Object();
- /**
- * The resource indicating what views to inflate to display the content of this
- * array adapter.
- */
- private int mResource;
- /**
- * The resource indicating what views to inflate to display the content of this
- * array adapter in a drop down widget.
- */
- private int mDropDownResource;
- /**
- * If the inflated resource is not a TextView, {@link #mFieldId} is used to find
- * a TextView inside the inflated views hierarchy. This field must contain the
- * identifier that matches the one defined in the resource file.
- */
- private int mFieldId = 0;
- /**
- * Indicates whether or not {@link #notifyDataSetChanged()} must be called whenever
- * {@link #mObjects} is modified.
- */
- private boolean mNotifyOnChange = true;
- private Context mContext;
- private ArrayList<T> mOriginalValues;
- private ArrayFilter mFilter;
- private LayoutInflater mInflater;
- /**
- * Constructor
- *
- * @param context The current context.
- * @param textViewResourceId The resource ID for a layout file containing a TextView to use when
- * instantiating views.
- */
- public CityAdapter(Context context, int textViewResourceId) {
- init(context, textViewResourceId, 0, new ArrayList<T>(),new ArrayList<T>());
- }
- /**
- * Constructor
- *
- * @param context The current context.
- * @param resource The resource ID for a layout file containing a layout to use when
- * instantiating views.
- * @param textViewResourceId The id of the TextView within the layout resource to be populated
- */
- public CityAdapter(Context context, int resource, int textViewResourceId) {
- init(context, resource, textViewResourceId, new ArrayList<T>(),new ArrayList<T>());
- }
- /**
- * Constructor
- *
- * @param context The current context.
- * @param textViewResourceId The resource ID for a layout file containing a TextView to use when
- * instantiating views.
- * @param objects The objects to represent in the ListView.
- * @param objects2 城市拼音数组
- */
- public CityAdapter(Context context, int textViewResourceId, T[] objects,T[] objects2) {
- init(context, textViewResourceId, 0, Arrays.asList(objects),Arrays.asList(objects2));
- }
- /**
- * Constructor
- *
- * @param context The current context.
- * @param resource The resource ID for a layout file containing a layout to use when
- * instantiating views.
- * @param textViewResourceId The id of the TextView within the layout resource to be populated
- * @param objects The objects to represent in the ListView.
- */
- public CityAdapter(Context context, int resource, int textViewResourceId, T[] objects,T[] objects2) {
- init(context, resource, textViewResourceId, Arrays.asList(objects),Arrays.asList(objects2));
- }
- /**
- * Constructor
- *
- * @param context The current context.
- * @param textViewResourceId The resource ID for a layout file containing a TextView to use when
- * instantiating views.
- * @param objects The objects to represent in the ListView.
- */
- public CityAdapter(Context context, int textViewResourceId, List<T> objects,List<T> objects2) {
- init(context, textViewResourceId, 0, objects,objects2);
- }
- /**
- * Constructor
- *
- * @param context The current context.
- * @param resource The resource ID for a layout file containing a layout to use when
- * instantiating views.
- * @param textViewResourceId The id of the TextView within the layout resource to be populated
- * @param objects The objects to represent in the ListView.
- */
- public CityAdapter(Context context, int resource, int textViewResourceId, List<T> objects,List<T> objects2) {
- init(context, resource, textViewResourceId, objects, objects2);
- }
- /**
- * Adds the specified object at the end of the array.
- *
- * @param object The object to add at the end of the array.
- */
- public void add(T object) {
- if (mOriginalValues != null) {
- synchronized (mLock) {
- mOriginalValues.add(object);
- if (mNotifyOnChange) notifyDataSetChanged();
- }
- } else {
- mObjects.add(object);
- if (mNotifyOnChange) notifyDataSetChanged();
- }
- }
- /**
- * Inserts the specified object at the specified index in the array.
- *
- * @param object The object to insert into the array.
- * @param index The index at which the object must be inserted.
- */
- public void insert(T object, int index) {
- if (mOriginalValues != null) {
- synchronized (mLock) {
- mOriginalValues.add(index, object);
- if (mNotifyOnChange) notifyDataSetChanged();
- }
- } else {
- mObjects.add(index, object);
- if (mNotifyOnChange) notifyDataSetChanged();
- }
- }
- /**
- * Removes the specified object from the array.
- *
- * @param object The object to remove.
- */
- public void remove(T object) {
- if (mOriginalValues != null) {
- synchronized (mLock) {
- mOriginalValues.remove(object);
- }
- } else {
- mObjects.remove(object);
- }
- if (mNotifyOnChange) notifyDataSetChanged();
- }
- /**
- * Remove all elements from the list.
- */
- public void clear() {
- if (mOriginalValues != null) {
- synchronized (mLock) {
- mOriginalValues.clear();
- }
- } else {
- mObjects.clear();
- }
- if (mNotifyOnChange) notifyDataSetChanged();
- }
- /**
- * Sorts the content of this adapter using the specified comparator.
- *
- * @param comparator The comparator used to sort the objects contained
- * in this adapter.
- */
- public void sort(Comparator<? super T> comparator) {
- Collections.sort(mObjects, comparator);
- if (mNotifyOnChange) notifyDataSetChanged();
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void notifyDataSetChanged() {
- super.notifyDataSetChanged();
- mNotifyOnChange = true;
- }
- /**
- * Control whether methods that change the list ({@link #add},
- * {@link #insert}, {@link #remove}, {@link #clear}) automatically call
- * {@link #notifyDataSetChanged}. If set to false, caller must
- * manually call notifyDataSetChanged() to have the changes
- * reflected in the attached view.
- *
- * The default is true, and calling notifyDataSetChanged()
- * resets the flag to true.
- *
- * @param notifyOnChange if true, modifications to the list will
- * automatically call {@link
- * #notifyDataSetChanged}
- */
- public void setNotifyOnChange(boolean notifyOnChange) {
- mNotifyOnChange = notifyOnChange;
- }
- private void init(Context context, int resource, int textViewResourceId, List<T> objects ,List<T> objects2) {
- mContext = context;
- mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mResource = mDropDownResource = resource;
- mObjects = objects;
- mObjects2 = objects2;
- mFieldId = textViewResourceId;
- }
- /**
- * Returns the context associated with this array adapter. The context is used
- * to create views from the resource passed to the constructor.
- *
- * @return The Context associated with this adapter.
- */
- public Context getContext() {
- return mContext;
- }
- /**
- * {@inheritDoc}
- */
- public int getCount() {
- return mObjects.size();
- }
- /**
- * {@inheritDoc}
- */
- public T getItem(int position) {
- return mObjects.get(position);
- }
- /**
- * Returns the position of the specified item in the array.
- *
- * @param item The item to retrieve the position of.
- *
- * @return The position of the specified item.
- */
- public int getPosition(T item) {
- return mObjects.indexOf(item);
- }
- /**
- * {@inheritDoc}
- */
- public long getItemId(int position) {
- return position;
- }
- /**
- * {@inheritDoc}
- */
- public View getView(int position, View convertView, ViewGroup parent) {
- return createViewFromResource(position, convertView, parent, mResource);
- }
- private View createViewFromResource(int position, View convertView, ViewGroup parent,
- int resource) {
- View view;
- TextView text;
- if (convertView == null) {
- view = mInflater.inflate(resource, parent, false);
- } else {
- view = convertView;
- }
- try {
- if (mFieldId == 0) {
- // If no custom field is assigned, assume the whole resource is a TextView
- text = (TextView) view;
- } else {
- // Otherwise, find the TextView field within the layout
- text = (TextView) view.findViewById(mFieldId);
- }
- } catch (ClassCastException e) {
- Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
- throw new IllegalStateException(
- "ArrayAdapter requires the resource ID to be a TextView", e);
- }
- text.setText(getItem(position).toString());
- return view;
- }
- /**
- * <p>Sets the layout resource to create the drop down views.</p>
- *
- * @param resource the layout resource defining the drop down views
- * @see #getDropDownView(int, android.view.View, android.view.ViewGroup)
- */
- public void setDropDownViewResource(int resource) {
- this.mDropDownResource = resource;
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public View getDropDownView(int position, View convertView, ViewGroup parent) {
- return createViewFromResource(position, convertView, parent, mDropDownResource);
- }
- /**
- * Creates a new ArrayAdapter from external resources. The content of the array is
- * obtained through {@link android.content.res.Resources#getTextArray(int)}.
- *
- * @param context The application's environment.
- * @param textArrayResId The identifier of the array to use as the data source.
- * @param textViewResId The identifier of the layout used to create views.
- *
- * @return An ArrayAdapter<CharSequence>.
- */
- public static ArrayAdapter<CharSequence> createFromResource(Context context,
- int textArrayResId, int textViewResId) {
- CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
- return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
- }
- /**
- * {@inheritDoc}
- */
- public Filter getFilter() {
- if (mFilter == null) {
- mFilter = new ArrayFilter();
- }
- return mFilter;
- }
- /**
- * <p>An array filter constrains the content of the array adapter with
- * a prefix. Each item that does not start with the supplied prefix
- * is removed from the list.</p>
- */
- private class ArrayFilter extends Filter {
- @Override
- protected FilterResults performFiltering(CharSequence prefix) {
- FilterResults results = new FilterResults();
- if (mOriginalValues == null) {
- synchronized (mLock) {
- mOriginalValues = new ArrayList<T>(mObjects);
- }
- }
- if (prefix == null || prefix.length() == 0) {
- synchronized (mLock) {
- ArrayList<T> list = new ArrayList<T>(mOriginalValues);
- results.values = list;
- results.count = list.size();
- }
- } else {
- String prefixString = prefix.toString().toLowerCase();
- final ArrayList<T> values = mOriginalValues;
- final int count = values.size();
- final ArrayList<T> newValues = new ArrayList<T>(count);
- for (int i = 0; i < count; i++) {
- final T value = values.get(i);
- final String valueText = value.toString().toLowerCase();
- final T value2 = mObjects2.get(i);
- final String valueText2 = value2.toString().toLowerCase();
-
- //查找拼音
- if(valueText2.startsWith(prefixString)){
- newValues.add(value);
- //查找汉字
- }else if(valueText.startsWith(prefixString)){
- newValues.add(value);
- }else{
-
- //添加汉字关联
- final String[] words = valueText.split(" ");
- final int wordCount = words.length;
-
- for (int k = 0; k < wordCount; k++) {
- if (words[k].startsWith(prefixString)) {
- newValues.add(value);
- break;
- }
- }
-
- //添加拼音关联汉字
- final String[] words2 = valueText2.split(" ");
- final int wordCount2 = words2.length;
- for (int k = 0; k < wordCount2; k++) {
- if (words2[k].startsWith(prefixString)) {
- newValues.add(value);
- break;
- }
- }
-
- }
-
- }
- results.values = newValues;
- results.count = newValues.size();
- }
- return results;
- }
- @SuppressWarnings("unchecked")
- @Override
- protected void publishResults(CharSequence constraint, FilterResults results) {
-
- mObjects = (List<T>) results.values;
- if (results.count > 0) {
- notifyDataSetChanged();
- } else {
- notifyDataSetInvalidated();
- }
- }
- }
- }
- cAdapter = new CityAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, 汉字数组,拼音数组);
- autoView.setAdapter(cAdapter);
- autoView.setThreshold(1);
- //其中 autoView 为 AutoCompleteTextView
发表评论
-
Android里的音量调节
2013-04-01 13:37 1318步骤1:或许系统音量 ... -
Android Camera 方法分析
2012-03-29 10:52 3483Android Camera源码分析 android通 ... -
MyCameraActivity
2012-03-29 10:10 674package cn.fn; import android. ... -
MyCameraActivity
2012-04-01 13:27 869package cn.fn; import android. ... -
android AutoCompleteTextView+ SQLite
2012-03-21 13:33 1090android AutoCompleteTextView+ S ... -
android 异步回调加载网络图片
2012-03-20 11:50 914在做应用的时候很多时候都会去从网络加载图片,而且还要做各种各样 ... -
ProgressBar+AsyncTask 实现界面数据异步加载
2012-03-20 10:09 1598ProgressBar+AsyncTask 实现界面数据异步 ... -
Android 网络图片异步加载实例
2012-03-20 10:04 950Android 网络图片异步加载实例 ... -
解决java.lang.OutOfMemoryError
2012-03-19 15:53 1064解决java.lang.OutOfMemoryError ... -
android Text 删除线
2012-03-16 19:35 1146import android.app.Activit ... -
android Gallery 详解
2012-03-14 14:17 1500android Gallery 正文 ... -
Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask,Task,AsynTask等
2012-03-14 11:43 1434Android的线程使用来更新UI----Thread、Han ... -
AsyncTask的使用
2012-03-14 10:59 786AsyncTask的使用 ... -
Android控件开发之Gallery3D效果
2012-03-13 14:38 2855Android控件开发之Gal ... -
android GridView
2012-03-08 10:28 902主类 import android.app.Activi ... -
ListView异步加载图片是非常实用的方法
2012-03-08 10:09 925ListView异步加载图片是非常实用的方法,凡是是要通过网络 ... -
复制assets下的数据库到SD卡
2012-03-07 10:56 1203首先使用sqliteadDev(一个windows下图形化sq ... -
Android异步加载图像小结
2012-03-06 16:46 589Android异步加载图像小结 (1)由于an ... -
AutoCompleteTextView
2012-03-05 14:53 1412AutoCompleteTextView ... -
android 动态加载List
2012-03-05 11:11 978main.xml <?xml ver ...
相关推荐
AutoCompleteTextView 显示更多
android API中的 AutoCompleteTextView组件只能匹配过滤纯英文或者纯汉字的 经过改进后可以输入 汉字拼音匹配汉字 类似于百度搜索 例如输入x 会匹配“迅雷”“迅速” 如果输入s 也可以匹配 “迅速”
AutoCompleteTextView汉字和拼音关联,可以输入汉字和拼音查询
本Demo展示了Android项目中AutoCompleteTextView以及MultiAutoCompleteTextView的简单使用,对其进行属性设置,以及数据填充。
扩展Android系统自带的AutoCompleteTextView控件,逐字提示,支持自定义类对象指定显示字段
autoCompleteTextView自定义BaseAdapter,实现下拉单的自定义样式,用sqlite数据库存放历史记录,进行写入时提示。点击某个下拉单写入autoCompleeTextView中。
AutoCompleteTextView自动完成文字输入 连接:http://i.cnblogs.com/EditPosts.aspx?opt=1
用AutoCompleteTextView实现中文和拼音关联自动提示
AutoCompleteTextView大多是采用ArrayAdapter进行适配的,这种有时候并不能满足我们的需求,所以就有了这个demo
AutoCompleteTextView按照关键字从服务器上获得数据显示下拉列表
AutoCompleteTextView+SQLite实现自动检索
值得一提的是,AutoCompleteTextView和MultiAutoCompleteTextView这两种自动提示框有什么区别呢,可以由上图看得出来,后者可以将字符串分割开来之后仍然能够自动提示,这是前者做不到的。教程地址:...
仿照Android上的AutoCompleteTextView(自动完成文本框)控件效果,在微信小程序上做的简易Demo,复杂功能需自行扩展和改造匹配规则,这里只是提供思路。 下载只要1分,尊重一下劳动成果! 没有分或者实在不想给分的小...
AutoCompleteTextView 自动提示联系人信息 还可以手动从通讯录选择联系人 选择完联系人,自动去除光标。
实现工具Android studio,一般AutoCompleteTextView提示语列表的适配器只能系统提供的ArrayAdapter等,这里自定义适配器继承了BaseAdapter。
自动填充文本实例,对Android自动文本填充AutoCompleteTextView的使用。
TestCompleteText_AutoCompleteTextView和TextWatcher结合使用的小栗子, 常见的安卓TextView输入显示关键词下拉提示.
只是一个很简单的例子,用autocompletetextview自动匹配姓名,在通讯录中查找,找到后点击会在下面的一个textview中显示出电话号码,适合初学者