android AutoCompleteTextView 自定义BaseAdapter

来源:互联网 发布:淘宝网服饰香柳儿 编辑:程序博客网 时间:2024/05/16 12:22


   今天我要实现AutoCompleteTextView 和自定义BaseAdapter的功能,当你在自定义BaseAdapter里面没有实现

Filterable的时候,AutoCompleteTextView就不能进行适配,这是大家都应该知道的时候,而且还会报错,错误就是前面的
[java] view plain copy
  1. package com.example.actv;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.example.actv.entity.PhoneContact;  
  7.   
  8. import android.os.Bundle;  
  9. import android.app.Activity;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.AdapterView.OnItemClickListener;  
  14. import android.widget.AutoCompleteTextView;  
  15.   
  16. public class MainActivity extends Activity implements OnItemClickListener {  
  17.     List<PhoneContact> mList;  
  18.     private AutoCompleteTextView mACTV;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.   
  25.         buildAppData();  
  26.         findView();  
  27.     }  
  28.   
  29.     private void buildAppData() {  
  30.         String[] names = { "abc""allen""bird""bike""book""cray",  
  31.                 "david""demon""eclipse""felling""frank""google",  
  32.                 "green""hill""hook","jin zhiwen""jack""jay""king","kevin","kobe",  
  33.                 "lily""lucy""mike""nike""nail""open","open cv",  
  34.                 "panda""pp""queue""ray allen""risk""tim cook","T-MAC","tony allen",  
  35.                 "x man""x phone""yy""world""w3c""zoom","zhu ziqing"};  
  36.           
  37.         mList = new ArrayList<PhoneContact>();  
  38.           
  39.         for (int i = 0; i < names.length; i++) {  
  40.             PhoneContact pc = new PhoneContact(100 + i, names[i], "1861234567"  
  41.                     + i, names[i].concat("@gmail.com"));  
  42.             mList.add(pc);  
  43.         }  
  44.   
  45.     }  
  46.   
  47.     private void findView() {  
  48.         mACTV = (AutoCompleteTextView) findViewById(R.id.mACTV);  
  49.         PhoneAdapter mAdapter = new PhoneAdapter(mList, getApplicationContext());  
  50.         mACTV.setAdapter(mAdapter);  
  51.         mACTV.setThreshold(1);  //设置输入一个字符 提示,默认为2  
  52.           
  53.         mACTV.setOnItemClickListener(this);  
  54.     }  
  55.   
  56.     @Override  
  57.     public boolean onCreateOptionsMenu(Menu menu) {  
  58.         // Inflate the menu; this adds items to the action bar if it is present.  
  59.         getMenuInflater().inflate(R.menu.main, menu);  
  60.         return true;  
  61.     }  
  62.   
  63.     @Override  
  64.     public void onItemClick(AdapterView<?> parent, View view, int position,  
  65.             long id) {  
  66.           
  67.         PhoneContact pc = mList.get(position);  
  68.         mACTV.setText(pc.getName()+" "+pc.getPhone());  
  69.     }  
  70.   
  71. }  

自定义Adapter

[java] view plain copy
  1. package com.example.actv;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.content.Context;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.Filter;  
  11. import android.widget.Filterable;  
  12. import android.widget.TextView;  
  13.   
  14. import com.example.actv.entity.PhoneContact;  
  15.   
  16. public class PhoneAdapter extends BaseAdapter implements Filterable {  
  17.     private ArrayFilter mFilter;  
  18.     private List<PhoneContact> mList;  
  19.     private Context context;  
  20.     private ArrayList<PhoneContact> mUnfilteredData;  
  21.       
  22.     public PhoneAdapter(List<PhoneContact> mList, Context context) {  
  23.         this.mList = mList;  
  24.         this.context = context;  
  25.     }  
  26.   
  27.     @Override  
  28.     public int getCount() {  
  29.           
  30.         return mList==null ? 0:mList.size();  
  31.     }  
  32.   
  33.     @Override  
  34.     public Object getItem(int position) {  
  35.         // TODO Auto-generated method stub  
  36.         return mList.get(position);  
  37.     }  
  38.   
  39.     @Override  
  40.     public long getItemId(int position) {  
  41.           
  42.         return position;  
  43.     }  
  44.   
  45.     @Override  
  46.     public View getView(int position, View convertView, ViewGroup parent) {  
  47.         View view;  
  48.         ViewHolder holder;  
  49.         if(convertView==null){  
  50.             view = View.inflate(context, R.layout.phone_item, null);  
  51.               
  52.             holder = new ViewHolder();  
  53.             holder.tv_name = (TextView) view.findViewById(R.id.tv_name);  
  54.             holder.tv_phone = (TextView) view.findViewById(R.id.tv_phone);  
  55.             holder.tv_email = (TextView) view.findViewById(R.id.tv_email);  
  56.               
  57.             view.setTag(holder);  
  58.         }else{  
  59.             view = convertView;  
  60.             holder = (ViewHolder) view.getTag();  
  61.         }  
  62.           
  63.         PhoneContact pc = mList.get(position);  
  64.           
  65.         holder.tv_name.setText("姓名:"+pc.getName());  
  66.         holder.tv_phone.setText("电话:"+pc.getPhone());  
  67.         holder.tv_email.setText("Email:"+pc.getEmail());  
  68.           
  69.         return view;  
  70.     }  
  71.       
  72.     static class ViewHolder{  
  73.         public TextView tv_name;  
  74.         public TextView tv_phone;  
  75.         public TextView tv_email;  
  76.     }  
  77.   
  78.     @Override  
  79.     public Filter getFilter() {  
  80.         if (mFilter == null) {  
  81.             mFilter = new ArrayFilter();  
  82.         }  
  83.         return mFilter;  
  84.     }  
  85.   
  86.     private class ArrayFilter extends Filter {  
  87.   
  88.         @Override  
  89.         protected FilterResults performFiltering(CharSequence prefix) {  
  90.             FilterResults results = new FilterResults();  
  91.   
  92.             if (mUnfilteredData == null) {  
  93.                 mUnfilteredData = new ArrayList<PhoneContact>(mList);  
  94.             }  
  95.   
  96.             if (prefix == null || prefix.length() == 0) {  
  97.                 ArrayList<PhoneContact> list = mUnfilteredData;  
  98.                 results.values = list;  
  99.                 results.count = list.size();  
  100.             } else {  
  101.                 String prefixString = prefix.toString().toLowerCase();  
  102.   
  103.                 ArrayList<PhoneContact> unfilteredValues = mUnfilteredData;  
  104.                 int count = unfilteredValues.size();  
  105.   
  106.                 ArrayList<PhoneContact> newValues = new ArrayList<PhoneContact>(count);  
  107.   
  108.                 for (int i = 0; i < count; i++) {  
  109.                     PhoneContact pc = unfilteredValues.get(i);  
  110.                     if (pc != null) {  
  111.                           
  112.                         if(pc.getName()!=null && pc.getName().startsWith(prefixString)){  
  113.                               
  114.                             newValues.add(pc);  
  115.                         }else if(pc.getEmail()!=null && pc.getEmail().startsWith(prefixString)){  
  116.                               
  117.                             newValues.add(pc);  
  118.                         }  
  119.                     }  
  120.                 }  
  121.   
  122.                 results.values = newValues;  
  123.                 results.count = newValues.size();  
  124.             }  
  125.   
  126.             return results;  
  127.         }  
  128.   
  129.         @Override  
  130.         protected void publishResults(CharSequence constraint,  
  131.                 FilterResults results) {  
  132.              //noinspection unchecked  
  133.             mList = (List<PhoneContact>) results.values;  
  134.             if (results.count > 0) {  
  135.                 notifyDataSetChanged();  
  136.             } else {  
  137.                 notifyDataSetInvalidated();  
  138.             }  
  139.         }  
  140.           
  141.     }  
  142. }  

注意:一定要实现 Filterable 接口,否则无效

MainActivity 布局文件

[html] view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <AutoCompleteTextView  
  8.         android:id="@+id/mACTV"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:hint="" >  
  12.   
  13.         <requestFocus />  
  14.     </AutoCompleteTextView>  
  15.   
  16. </RelativeLayout>  

phone_item.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/ivIcon"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:contentDescription="@string/app_name"  
  11.         android:src="@drawable/ic_launcher" />  
  12.   
  13.     <LinearLayout  
  14.         android:id="@+id/appInfo"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_marginLeft="5dip"  
  18.         android:layout_toRightOf="@id/ivIcon"  
  19.         android:orientation="vertical" >  
  20.   
  21.         <TextView  
  22.             android:id="@+id/tv_name"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="@string/person_name"  
  26.             android:textColor="#000000"  
  27.             android:textSize="16sp" />  
  28.   
  29.         <TextView  
  30.             android:id="@+id/tv_phone"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="@string/phone"  
  34.             android:textColor="#666666"  
  35.             android:textSize="13sp" />  
  36.   
  37.         <TextView  
  38.             android:id="@+id/tv_email"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="@string/email"  
  42.             android:textColor="#666666"  
  43.             android:textSize="13sp" />  
  44.     </LinearLayout>  
  45.   
  46.     <Button  
  47.         android:id="@+id/btnClick"  
  48.         android:layout_width="80dip"  
  49.         android:layout_height="wrap_content"  
  50.         android:layout_alignParentRight="true"  
  51.         android:layout_centerVertical="true"  
  52.         android:focusable="false"  
  53.         android:text="@string/call"  
  54.         android:textColor="#000000"  
  55.         android:textSize="16sp" />  
  56.   
  57. </RelativeLayout>  

0 0
原创粉丝点击