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

今天学到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. Win32汇编学习(6):键盘输入消息

    这次,我们将要学习WINDOWS程序是如何处理键盘消息的. 理论: 因为大多数的PC只有一个键盘,所以所有运行中的WINDOWS程序必须共用它.WINDOWS 将负责把击键消息送到具有输入焦点的那个应 ...

  2. vue 全局变量

    // g.vue <script> // 用于放置全局变量的组件 export default { test: 'test', } </script> // 方法一 g.vue ...

  3. WebApi实现验证授权Token,WebApi生成文档等(转)

    using System; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Security; ...

  4. HBase和MongoDB的区别

    Mongodb用于存储非结构化数据,尤其擅长存储json格式的数据.存储的量大概在10亿级别,再往上性能就下降了,除非另外分库.Hbase是架构在hdfs上的列式存储,擅长rowkey的快速查询,但模 ...

  5. 实现Python与STM32通信

    断断续续学了几周Stm32后,突然想实现上位机和下位机的通信,恰好自己学过一点python,便想通过python实现通信. 在网上看见python库pyserial可以实现此功能,便去官网找了一下 , ...

  6. JS实时获取输入框中的值

    实时获取input输入框中的值需要oninput和onpropertychange属性来实现.原因是onpropertychange属性为IE专属,而oninput属性支持大部分浏览器包括IE9及以上 ...

  7. [easyUI] datagrid 数据格 可以进行分页

    1. 新建一个GridNode的类: public class GridNode { private Long id; private String title;//投票标题 private Inte ...

  8. 依赖注入demo

    让我们看一个例子: class UserProvider{ protected $connection; public function __construct(){ $this->connec ...

  9. 『PyTorch』第五弹_深入理解Tensor对象_中下:数学计算以及numpy比较_&_广播原理简介

    一.简单数学操作 1.逐元素操作 t.clamp(a,min=2,max=4)近似于tf.clip_by_value(A, min, max),修剪值域. a = t.arange(0,6).view ...

  10. xml字符串,xml对象,数组之间的相互转化

    <?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { pu ...