android AutoCompleteTextView 自定义BaseAdapter

最近项目中需要做搜索功能,实现类似 Google、Baidu 搜索的 下拉提示效果。Android为我们提供了 AutoCompleteTextView 控件来完成此功能。

网上好多例子都是简单使用 ArrayAdapter 来实现的,界面比较简单,实际项目中用处不大;自己研究了一番,自定义Adapter 继承BaseAdapter 并实现Filterable 接口 实现了上述功能。

运行效果截图

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);  //设置输入一个字符 提示,默认为2  
          
        mACTV.setOnItemClickListener(this);  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        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

package com.example.actv;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import android.content.Context;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.Filter;  
import android.widget.Filterable;  
import android.widget.TextView;  
  
import com.example.actv.entity.PhoneContact;  
  
public class PhoneAdapter extends BaseAdapter implements Filterable {  
    private ArrayFilter mFilter;  
    private List<PhoneContact> mList;  
    private Context context;  
    private ArrayList<PhoneContact> mUnfilteredData;  
      
    public PhoneAdapter(List<PhoneContact> mList, Context context) {  
        this.mList = mList;  
        this.context = context;  
    }  
  
    @Override  
    public int getCount() {  
          
        return mList==null ? 0:mList.size();  
    }  
  
    @Override  
    public Object getItem(int position) {  
        // TODO Auto-generated method stub  
        return mList.get(position);  
    }  
  
    @Override  
    public long getItemId(int position) {  
          
        return position;  
    }  
  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        View view;  
        ViewHolder holder;  
        if(convertView==null){  
            view = View.inflate(context, R.layout.phone_item, null);  
              
            holder = new ViewHolder();  
            holder.tv_name = (TextView) view.findViewById(R.id.tv_name);  
            holder.tv_phone = (TextView) view.findViewById(R.id.tv_phone);  
            holder.tv_email = (TextView) view.findViewById(R.id.tv_email);  
              
            view.setTag(holder);  
        }else{  
            view = convertView;  
            holder = (ViewHolder) view.getTag();  
        }  
          
        PhoneContact pc = mList.get(position);  
          
        holder.tv_name.setText("姓名:"+pc.getName());  
        holder.tv_phone.setText("电话:"+pc.getPhone());  
        holder.tv_email.setText("Email:"+pc.getEmail());  
          
        return view;  
    }  
      
    static class ViewHolder{  
        public TextView tv_name;  
        public TextView tv_phone;  
        public TextView tv_email;  
    }  
  
    @Override  
    public Filter getFilter() {  
        if (mFilter == null) {  
            mFilter = new ArrayFilter();  
        }  
        return mFilter;  
    }  
  
    private class ArrayFilter extends Filter {  
  
        @Override  
        protected FilterResults performFiltering(CharSequence prefix) {  
            FilterResults results = new FilterResults();  
  
            if (mUnfilteredData == null) {  
                mUnfilteredData = new ArrayList<PhoneContact>(mList);  
            }  
  
            if (prefix == null || prefix.length() == 0) {  
                ArrayList<PhoneContact> list = mUnfilteredData;  
                results.values = list;  
                results.count = list.size();  
            } else {  
                String prefixString = prefix.toString().toLowerCase();  
  
                ArrayList<PhoneContact> unfilteredValues = mUnfilteredData;  
                int count = unfilteredValues.size();  
  
                ArrayList<PhoneContact> newValues = new ArrayList<PhoneContact>(count);  
  
                for (int i = 0; i < count; i++) {  
                    PhoneContact pc = unfilteredValues.get(i);  
                    if (pc != null) {  
                          
                        if(pc.getName()!=null && pc.getName().startsWith(prefixString)){  
                              
                            newValues.add(pc);  
                        }else if(pc.getEmail()!=null && pc.getEmail().startsWith(prefixString)){  
                              
                            newValues.add(pc);  
                        }  
                    }  
                }  
  
                results.values = newValues;  
                results.count = newValues.size();  
            }  
  
            return results;  
        }  
  
        @Override  
        protected void publishResults(CharSequence constraint,  
                FilterResults results) {  
             //noinspection unchecked  
            mList = (List<PhoneContact>) results.values;  
            if (results.count > 0) {  
                notifyDataSetChanged();  
            } else {  
                notifyDataSetInvalidated();  
            }  
        }  
          
    }  
}  

注意:一定要实现 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 条评论
登录 后参与评论

相关文章

扫描关注云+社区