AutoCompleteTextView配合自定义的CursorAdapter(setAdapter()),可以帮助我们完成查找的功能.关键就在于类CursorAdapter.
CursorAdapter是继承自BaseAdapter并且实现了接口Filterable 。所以在我们自己定义的CursorAdapter子类中就不需要在继承Filterable,但对于JRE是1.5以下的虚拟机来说。是要重写方法getFilter()的;
要继承CursorAdapter类。必须实现的方法有:
1)首先执行的是public Cursor runQueryOnBackgroundThread(CharSequence constraint),constraint就是我们输入的要查询的关键字;此方法产生查询到的所有数据的cursor.并将其返回给下一个函数;
2)接下来执行方法public View newView(Context context, Cursor cursor, ViewGroup parent),cursor就是有第一种方法产生的.这个方法主要是产生一个个具体的承载cursor指向的数据的view类,最常见的是TextView;
3)接下来执行方法public void bindView(View view, Context context, Cursor cursor) 。view就是第2步产生的。cursor是第一步产生的。显而易见,就是将两者进行绑定。
但要注意的是,2和3是反复交替执行的。产生多少条数据(cursor.getcount())就执行多少轮。还有一点容易忽视的就是在xml文件中定义AutoCompleteTextView片段中一定要加入以下代码android:completionThreshold="1",他表示你最少要输入关键字的个数;
下面的代码以查询联系人为例:
package com.zymic.home; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.os.Bundle; import android.provider.Contacts; import android.provider.Contacts.People; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AutoCompleteTextView; import android.widget.CursorAdapter; import android.widget.Filterable; import android.widget.TextView; import android.widget.Toast; public class AutoTextViewEx extends Activity { private AutoCompleteTextView autoTextView; private Cursor cursor; // private static final String[] PEOPLE_PROJECTION = new String[] { Contacts.People._ID, Contacts.People.PRIMARY_PHONE_ID, Contacts.People.TYPE, Contacts.People.NUMBER, Contacts.People.LABEL, Contacts.People.NAME, }; // @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // autoTextView=(AutoCompleteTextView)findViewById(R.id.autotextview); cursor=getApplicationContext().getContentResolver().query(People.CONTENT_URI, PEOPLE_PROJECTION, null, null, null); MyAdapter adapter=new MyAdapter(this,cursor); autoTextView.setAdapter(adapter); } // public class MyAdapter extends CursorAdapter implements Filterable{ Cursor cursor; public MyAdapter(Context context, Cursor c) { super(context, c); // TODO Auto-generated constructor stub } @Override public void bindView(View view, Context context, Cursor cursor) { ((TextView)view).setText(cursor.getString(5)); Toast.makeText(getApplicationContext(), "bindView", Toast.LENGTH_SHORT).show(); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater)context.getSystemService ( Context.LAYOUT_INFLATER_SERVICE); TextView view=(TextView) inflater.inflate(android.R.layout.simple_dropdown_item_1line, null); //view.setText(cursor.getString(5)); Toast.makeText(getApplicationContext(), "newView", Toast.LENGTH_SHORT).show(); return view; } @Override public Cursor runQueryOnBackgroundThread(CharSequence constraint) { //UPPER(Contacts.People.NAME) String where="UPPER("+Contacts.People.NAME+") GLOB ?"; String[]to=new String[]{"*"+constraint.toString().toUpperCase()+"*"}; cursor=getApplicationContext().getContentResolver().query(People.CONTENT_URI, PEOPLE_PROJECTION, where, to, null); //System.out.println(cursor); Toast.makeText(getApplicationContext(), "runQuery", Toast.LENGTH_SHORT).show(); return cursor; } } }