1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.example.android.apis.view; 18 19import com.example.android.apis.R; 20 21import android.app.Activity; 22import android.content.ContentResolver; 23import android.content.Context; 24import android.database.Cursor; 25import android.net.Uri; 26import android.os.Bundle; 27import android.provider.ContactsContract.Contacts; 28import android.view.LayoutInflater; 29import android.view.View; 30import android.view.ViewGroup; 31import android.widget.AutoCompleteTextView; 32import android.widget.CursorAdapter; 33import android.widget.FilterQueryProvider; 34import android.widget.Filterable; 35import android.widget.TextView; 36 37public class AutoComplete4 extends Activity { 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.autocomplete_4); 42 43 ContentResolver content = getContentResolver(); 44 Cursor cursor = content.query(Contacts.CONTENT_URI, 45 CONTACT_PROJECTION, null, null, null); 46 47 ContactListAdapter adapter = new ContactListAdapter(this, cursor); 48 49 AutoCompleteTextView textView = (AutoCompleteTextView) 50 findViewById(R.id.edit); 51 textView.setAdapter(adapter); 52 } 53 54 // XXX compiler bug in javac 1.5.0_07-164, we need to implement Filterable 55 // to make compilation work 56 public static class ContactListAdapter extends CursorAdapter implements Filterable { 57 public ContactListAdapter(Context context, Cursor c) { 58 super(context, c); 59 mContent = context.getContentResolver(); 60 } 61 62 @Override 63 public View newView(Context context, Cursor cursor, ViewGroup parent) { 64 final LayoutInflater inflater = LayoutInflater.from(context); 65 final TextView view = (TextView) inflater.inflate( 66 android.R.layout.simple_dropdown_item_1line, parent, false); 67 view.setText(cursor.getString(COLUMN_DISPLAY_NAME)); 68 return view; 69 } 70 71 @Override 72 public void bindView(View view, Context context, Cursor cursor) { 73 ((TextView) view).setText(cursor.getString(COLUMN_DISPLAY_NAME)); 74 } 75 76 @Override 77 public String convertToString(Cursor cursor) { 78 return cursor.getString(COLUMN_DISPLAY_NAME); 79 } 80 81 @Override 82 public Cursor runQueryOnBackgroundThread(CharSequence constraint) { 83 FilterQueryProvider filter = getFilterQueryProvider(); 84 if (filter != null) { 85 return filter.runQuery(constraint); 86 } 87 88 Uri uri = Uri.withAppendedPath( 89 Contacts.CONTENT_FILTER_URI, 90 Uri.encode(constraint.toString())); 91 return mContent.query(uri, CONTACT_PROJECTION, null, null, null); 92 } 93 94 private ContentResolver mContent; 95 } 96 97 public static final String[] CONTACT_PROJECTION = new String[] { 98 Contacts._ID, 99 Contacts.DISPLAY_NAME 100 }; 101 102 private static final int COLUMN_DISPLAY_NAME = 1; 103}