最近在学习安卓开发,开始是看视频学的,基本上是照着老师的操作来,但其实老师也是按照安卓的开发文档来教的,于是决定试试自己看文档来学。

今天学到AutoCompleteTextView,一上来先按照ListView的操作流程:

1.获取对象

2.创建Adapter对象实现BaseAdapter接口

3.setAdapter

结果发现这不行。。因为从源码中可以看到adapter参数必须是一个filterable list adapter!

 /**
* <p>Changes the list of data used for auto completion. The provided list
* must be a filterable list adapter.</p>
*
* <p>The caller is still responsible for managing any resources used by the adapter.
* Notably, when the AutoCompleteTextView is closed or released, the adapter is not notified.
* A common case is the use of {@link android.widget.CursorAdapter}, which
* contains a {@link android.database.Cursor} that must be closed. This can be done
* automatically (see
* {@link android.app.Activity#startManagingCursor(android.database.Cursor)
* startManagingCursor()}),
* or by manually closing the cursor when the AutoCompleteTextView is dismissed.</p>
*
* @param adapter the adapter holding the auto completion data
*
* @see #getAdapter()
* @see android.widget.Filterable
* @see android.widget.ListAdapter
*/
public <T extends ListAdapter & Filterable> void setAdapter(T adapter) {
if (mObserver == null) {
mObserver = new PopupDataSetObserver();
} else if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(mObserver);
}
mAdapter = adapter;
if (mAdapter != null) {
//noinspection unchecked
mFilter = ((Filterable) mAdapter).getFilter();
adapter.registerDataSetObserver(mObserver);
} else {
mFilter = null;
} mPopup.setAdapter(mAdapter);
}

官方文档是这么写的

1.Add the AutoCompleteTextView to your layout. Here's a layout with only the text field:

<?xml version="1.0" encoding="utf-8"?>
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/autocomplete_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

2.Define the array that contains all text suggestions. For example, here's an array of country names that's defined in an XML resource file (res/values/strings.xml):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries_array">
<item>Afghanistan</item>
<item>Albania</item>
<item>Algeria</item>
<item>American Samoa</item>
<item>Andorra</item>
<item>Angola</item>
<item>Anguilla</item>
<item>Antarctica</item>
...
</string-array>
</resources>

3.In your Activity or Fragment, use the following code to specify the adapter that supplies the suggestions:

// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
// Get the string array
String[] countries = getResources().getStringArray(R.array.countries_array);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
textView.setAdapter(adapter);
Here, a new ArrayAdapter is initialized to bind each item in the COUNTRIES string array to a TextView that exists in the simple_list_item_1 layout (this is a layout provided by Android that provides a standard appearance for text in a list).
Then assign the adapter to the AutoCompleteTextView by calling setAdapter().

那么问题来了,这是从xml中读取的列表,如果我要自己生成列表怎么做呢?如果看源码就很清楚了

/**
* 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 ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
init(context, resource, textViewResourceId, Arrays.asList(objects));
}

这是ArrayAdapter的构造方法,所以我们还要创建一个simple_list_item_1.xml文件,在这个布局中写入一个TextView,可以设置其ID为textView1,那么此时我们就可以创建一个ArrayAdapter的实例化对象了

// text变量用来存储提示用户输入的列表
String[] text = { "a", "ab", "abc", "abcd" };
// 创建ArrayAdapter对象
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item, R.id.textView1, text);

按照上面方法即可实现带输入提示的输入框。

----------------------------------------------------------------------------------------------------------------------------------------

刚刚看到Spinner控件的时候遇到了同样的问题,才发现上面的处理方法其实是有问题的,也就是按照官方文档说的simple_list_item_1.xml其实是不需要自己创建的,这是安卓的资源文件里面自带的,但是要注意的是这个文件的目录在android.R.layout.simple_list_item_1,关键在前面的android.R,也就是说这个文件不在当前工程目录下,在安卓提供的R文件中定义了常量。

安卓开发学习之AutoCompleteTextView的更多相关文章

  1. 安卓开发学习笔记(三):Android Stuidio无法引用Intent来创建对象,出现cannot resolve xxx

    笔者在进行安卓开发时,发现自己的代码语法完全没有问题.尤其是创建intent对象的时候,语法完全是正确的,但是Android Stuidio却显示报错,Intent类显示为红色,如图所示: 代码如下所 ...

  2. 安卓开发学习之Menu

    安卓开发中菜单是一个很重要的组件,从安卓开发文档(http://wear.techbrood.com/guide/index.html)中可以看到,安卓UI设计中的Menu主要分为: A.Option ...

  3. 安卓开发学习日记 DAY1

    1.eclipse安装,很简单 2.安卓sdk manager 下载安装 sdk manager是一个安卓开发所使用的sdk文件的管理程序,可以使用这个程序在官网上下载相应的安卓的api等.因为需要在 ...

  4. 安卓开发学习历程1——《第一行代码》coolweather项目setOnItemClickListener函数,Sql语句修改对模拟app程序机影响

    今天,将<第一行代码>最后实战的coolweather项目,认真做了一遍. 今晚,在书中第一阶段开发代码认眞在Android studio敲完,发现setOnItemClickListen ...

  5. 安卓开发学习笔记(五):史上最简单且华丽地实现Android Stutio当中Webview控件https/http协议的方法

    一.我们先在XML当中自定义一个webview(Second_layout.xml) 代码如下: <?xml version="1.0" encoding="utf ...

  6. 安卓开发学习笔记(四):Android Stuidio无法实现隐式Intent是为什么?

    一.首先检查我们的代码: FirstActivity.java(主活动程序当中的代码):Button3监听器后面的代码就是我们隐式Intent的业务逻辑所在了,大家可以往下面看看,大概在代码的第57行 ...

  7. 安卓开发学习日记 DAY5——监听事件onClick的实现方法

    今天主要学习了监听事件的是实现方法,就是说,做了某些动作后,怎么监听这个动作并作出相应反应. 方法主要有三种: 1.匿名内部类的方法 2.独立类的方法 3.类似实现接口的方法 以下分别分析: 1.匿名 ...

  8. 安卓开发学习日记 DAY3——TextView,EditView,ImageView

    今天学习了一些控件的使用方法,包括TextView,EditView,ImageView 1.TextView,输出一个文本呗 主要属性有 android:id 标志 android:layout_w ...

  9. 安卓开发学习日记 DAY2——android项目文件

    当一个android项目建立时,会有一个目录,以下为目录所包含内容 src:放置java源代码 gen:基本不会做任何更改,放置自动生成的配置文件(主要是R文件) Android4.4.2:放置当前版 ...

随机推荐

  1. Idea 全局替换指定字符

    最近使用idea开发,刚接触不久,然后碰到需要全局替换的时候,懵逼了.之前使用eclipse 直接Ctrl+F 就可以操作了. 现在使用idea 摁Ctrl+F竟然只能搜,不能替换....尴尬的一匹. ...

  2. [c/c++] programming之路(20)、字符串(一)

    一.字符串 #include<stdio.h> #include<stdlib.h> void main(){ ]="notepad"; printf(&q ...

  3. 在Linux下OpenCV的下载和编译

    原理上来说,和windows下没有差别,我们同样使用Cmake-GUI来解决问题. 我们推荐QT和OpenCV全部采用官方的方式重新安装一遍,否则可能会丢失一些模块,而这些都会降低开发效率. 1.参考 ...

  4. 剑指offer(14)链表中倒数第K个节点

    题目描述 输入一个链表,输出该链表中倒数第k个节点. 题目分析 用两个指针来跑,两个指针中间相距k-1个节点,第一个指针先跑,跑到了第k个节点时,第二个指针则是第一个节点. 这时候两个一起跑.当第一个 ...

  5. JS设计模式(1)单例模式

    什么是单例模式 定义:1.只有一个实例.2.可以全局访问 主要解决:一个全局使用的类频繁地创建与销毁. 何时使用:当您想控制实例数目,节省系统资源的时候. 如何解决:判断系统是否已经有这个单例,如果有 ...

  6. 【Entity Framework】Model First Approach

    EF中的model first 所谓mf, 就是使用vs提供的edm designer去设计model,然后将设计好的model使用vs在指定的数据库中生成数据库即可. 当你的项目既没有数据库也没有c ...

  7. 浅谈Cocos2d-js cc.director

    在cocos2d-x里面,游戏的任何时间,只有一个场景对象实例处于运行状态,该对象可以作为当前游戏内容的整体包对象. 环境设定 进入游戏之前,导演会设置游戏的运行环境: 设置游戏视图,包含视图的投射, ...

  8. 【读书笔记】Cronjob原理及源码分析

    原文链接:https://mp.weixin.qq.com/s?__biz=MzI0NjI4MDg5MQ==&mid=2715291842&idx=1&sn=e605f9b40 ...

  9. python __init__.py 的作用

    __init__.py的主要作用是: . Python中package的标识,不能删除 . 定义__all__用来模糊导入 . 编写Python代码(不建议在__init__中写python模块,可以 ...

  10. ORA-01034 报错

    问题描述: 执行任何DB语句都会有如下报错: Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.ERROR:ORA-0 ...