Home | History | Annotate | Download | only in view
      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 
     17 package com.example.android.apis.view;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.content.ContentResolver;
     23 import android.content.Context;
     24 import android.database.Cursor;
     25 import android.net.Uri;
     26 import android.os.Bundle;
     27 import android.provider.ContactsContract.Contacts;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 import android.widget.AutoCompleteTextView;
     32 import android.widget.CursorAdapter;
     33 import android.widget.FilterQueryProvider;
     34 import android.widget.Filterable;
     35 import android.widget.TextView;
     36 
     37 public 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 }