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.os.Bundle;
     26 import android.provider.Contacts;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.widget.AutoCompleteTextView;
     31 import android.widget.CursorAdapter;
     32 import android.widget.Filterable;
     33 import android.widget.TextView;
     34 
     35 public class AutoComplete4 extends Activity {
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         setContentView(R.layout.autocomplete_4);
     40 
     41         ContentResolver content = getContentResolver();
     42         Cursor cursor = content.query(Contacts.People.CONTENT_URI,
     43                 PEOPLE_PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER);
     44         ContactListAdapter adapter = new ContactListAdapter(this, cursor);
     45 
     46         AutoCompleteTextView textView = (AutoCompleteTextView)
     47                 findViewById(R.id.edit);
     48         textView.setAdapter(adapter);
     49     }
     50 
     51     // XXX compiler bug in javac 1.5.0_07-164, we need to implement Filterable
     52     // to make compilation work
     53     public static class ContactListAdapter extends CursorAdapter implements Filterable {
     54         public ContactListAdapter(Context context, Cursor c) {
     55             super(context, c);
     56             mContent = context.getContentResolver();
     57         }
     58 
     59         @Override
     60         public View newView(Context context, Cursor cursor, ViewGroup parent) {
     61             final LayoutInflater inflater = LayoutInflater.from(context);
     62             final TextView view = (TextView) inflater.inflate(
     63                     android.R.layout.simple_dropdown_item_1line, parent, false);
     64             view.setText(cursor.getString(5));
     65             return view;
     66         }
     67 
     68         @Override
     69         public void bindView(View view, Context context, Cursor cursor) {
     70             ((TextView) view).setText(cursor.getString(5));
     71         }
     72 
     73         @Override
     74         public String convertToString(Cursor cursor) {
     75             return cursor.getString(5);
     76         }
     77 
     78         @Override
     79         public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
     80             if (getFilterQueryProvider() != null) {
     81                 return getFilterQueryProvider().runQuery(constraint);
     82             }
     83 
     84             StringBuilder buffer = null;
     85             String[] args = null;
     86             if (constraint != null) {
     87                 buffer = new StringBuilder();
     88                 buffer.append("UPPER(");
     89                 buffer.append(Contacts.ContactMethods.NAME);
     90                 buffer.append(") GLOB ?");
     91                 args = new String[] { constraint.toString().toUpperCase() + "*" };
     92             }
     93 
     94             return mContent.query(Contacts.People.CONTENT_URI, PEOPLE_PROJECTION,
     95                     buffer == null ? null : buffer.toString(), args,
     96                     Contacts.People.DEFAULT_SORT_ORDER);
     97         }
     98 
     99         private ContentResolver mContent;
    100     }
    101 
    102     private static final String[] PEOPLE_PROJECTION = new String[] {
    103         Contacts.People._ID,
    104         Contacts.People.PRIMARY_PHONE_ID,
    105         Contacts.People.TYPE,
    106         Contacts.People.NUMBER,
    107         Contacts.People.LABEL,
    108         Contacts.People.NAME,
    109     };
    110 }
    111