ArrayAdapters使用注意事项:
ArrayAdapters adapter=new ArrayAdapters(context,int textViewResourceld ,int resource);其中context指上下文, textViewResourceld参数控制每个列表项 的组件,注意是控制列表项组件,换句话说就是下菜单的显示样式,android系统自带有几种显示样式,android.R.layout_array_item android.R.layout.checked_item,;第三个参数负责为列表项提供数据。如下有一自动完成文本框AutoCompleteTextView,用于实现允许用户输入一定字符后显示一个下拉菜单,供用户 从中选择。
布局代码:
<AutoCompleteTextView
android:id="@+id/searchtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:completionHint="请输入搜素内容"
android:completionThreshold="2"
android:layout_weight="6"
android:dropDownHorizontalOffset="2px"
android:dropDownVerticalOffset="2px"
/>
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜素"
android:layout_weight="1"
android:layout_marginLeft="10px"/>
Java代码
定义下菜单显示的内容
private static final String[] COUNTRIES=new String[]{"明日科技","明日科技有限公司",
"吉林明日科技有限公司","明日编程词典","明日辉煌","明日欢喜","明日"};
adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1(这个资源是android系统自带的),COUNTRIES);
searchtext.setAdapter(adapter);
解释说明:上面代码中ArrayAdapter的第二个参数并不是R.id.searchtext,searchtext组件不是用来显示列表组件的 。