首页 > 代码库 > SimpleAdapter和AutoCompleteTextView配合使用
SimpleAdapter和AutoCompleteTextView配合使用
修改后成为这样→
package com.kale.autocompletetextview;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.MultiAutoCompleteTextView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { AutoCompleteTextView acTv,sacTv; MultiAutoCompleteTextView mcTv; String []books = new String[] { "android", "java", "xml", "Ant" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个adapter来封装数组 ArrayAdapter<String> aa = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,books); acTv = (AutoCompleteTextView)findViewById(R.id.auto_autoCompleteTv_id); acTv.setAdapter(aa); mcTv = (MultiAutoCompleteTextView)findViewById(R.id.multi_autoCompleteTv_id); mcTv.setAdapter(aa); //为其设置分割符 mcTv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); sacTv = (AutoCompleteTextView)findViewById(R.id.simple_autoCompleteTv_id); ArrayList<Map<String, String>> list = new ArrayList<Map<String,String>>(); addItems(list); SimpleAdapter phone = new SimpleAdapter( this, list, R.layout.item, new String[] { "text", "name"}, new int[] { R.id.text_textView_id, R.id.name_textView_id } ); sacTv.setAdapter(phone); sacTv.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View parent, int position,long id) { TextView tv = (TextView)parent.findViewById(R.id.name_textView_id); sacTv.setText(tv.getText().toString()+" "); sacTv.setSelection((sacTv.getText().toString()).length()); } }); } private void addItems(ArrayList<Map<String,String>> list) { HashMap<String,String> item; /*所有的搜索词语拼接成brandSearchText字段,用空格隔开。 比如:“诺基亚 NOKIA njy nuojiya”,把它作为搜索词,以便提高效率 但我们不想要把所有的相关词显示,我们只需要显示主要的那个,即:name的值。 text和name键对应的值都是候选项,但我们只显示的是name中的文字。*/ item = new HashMap<String,String>(); item.put( "text", "NOKIA nuojiya NJY"); item.put( "name", "诺基亚"); list.add( item ); item = new HashMap<String,String>(); item.put( "text", "SAMSUNG SX sanxing"); item.put( "name", "三星"); list.add( item ); item = new HashMap<String,String>(); item.put( "text", "摩托罗拉 moto MTLL motuoluola motoloar"); item.put( "name", "摩托罗拉"); list.add( item ); }}
xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp"> <TextView android:id="@+id/text_textView_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Medium Text" android:visibility="gone" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/name_textView_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textColor="#0000ff" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /></LinearLayout>
源码下载:http://download.csdn.net/detail/shark0017/7647965
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。