今天我要实现AutoCompleteTextView 和自定义BaseAdapter的功能,当你在自定义BaseAdapter里面没有实现
Filterable的时候,AutoCompleteTextView就不能进行适配,这是大家都应该知道的时候,而且还会报错,错误就是前面的
- package com.example.actv;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import com.example.actv.entity.PhoneContact;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AutoCompleteTextView;
-
- public class MainActivity extends Activity implements OnItemClickListener {
- List<PhoneContact> mList;
- private AutoCompleteTextView mACTV;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- buildAppData();
- findView();
- }
-
- private void buildAppData() {
- String[] names = { "abc", "allen", "bird", "bike", "book", "cray",
- "david", "demon", "eclipse", "felling", "frank", "google",
- "green", "hill", "hook","jin zhiwen", "jack", "jay", "king","kevin","kobe",
- "lily", "lucy", "mike", "nike", "nail", "open","open cv",
- "panda", "pp", "queue", "ray allen", "risk", "tim cook","T-MAC","tony allen",
- "x man", "x phone", "yy", "world", "w3c", "zoom","zhu ziqing"};
-
- mList = new ArrayList<PhoneContact>();
-
- for (int i = 0; i < names.length; i++) {
- PhoneContact pc = new PhoneContact(100 + i, names[i], "1861234567"
- + i, names[i].concat("@gmail.com"));
- mList.add(pc);
- }
-
- }
-
- private void findView() {
- mACTV = (AutoCompleteTextView) findViewById(R.id.mACTV);
- PhoneAdapter mAdapter = new PhoneAdapter(mList, getApplicationContext());
- mACTV.setAdapter(mAdapter);
- mACTV.setThreshold(1);
-
- mACTV.setOnItemClickListener(this);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
-
- PhoneContact pc = mList.get(position);
- mACTV.setText(pc.getName()+" "+pc.getPhone());
- }
-
- }
自定义Adapter
注意:一定要实现 Filterable 接口,否则无效
MainActivity 布局文件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
-
- <AutoCompleteTextView
- android:id="@+id/mACTV"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="" >
-
- <requestFocus />
- </AutoCompleteTextView>
-
- </RelativeLayout>
phone_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
-
- <ImageView
- android:id="@+id/ivIcon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:contentDescription="@string/app_name"
- android:src="@drawable/ic_launcher" />
-
- <LinearLayout
- android:id="@+id/appInfo"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dip"
- android:layout_toRightOf="@id/ivIcon"
- android:orientation="vertical" >
-
- <TextView
- android:id="@+id/tv_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/person_name"
- android:textColor="#000000"
- android:textSize="16sp" />
-
- <TextView
- android:id="@+id/tv_phone"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/phone"
- android:textColor="#666666"
- android:textSize="13sp" />
-
- <TextView
- android:id="@+id/tv_email"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/email"
- android:textColor="#666666"
- android:textSize="13sp" />
- </LinearLayout>
-
- <Button
- android:id="@+id/btnClick"
- android:layout_width="80dip"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:focusable="false"
- android:text="@string/call"
- android:textColor="#000000"
- android:textSize="16sp" />
-
- </RelativeLayout>
0 0