It work if I use a TextView instead of my custo layout (see my first
post). Don't ask me why.
So I cheated and set the picture of the contact as the background of
the text view.
The newView method now look like this :

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup
parent) {
        float density =
context.getResources().getDisplayMetrics().density;
        TextView name = new TextView(context);
        name.setText(cursor.getString(1));
        name.setTag(cursor.getLong(0));
        name.setGravity(Gravity.CENTER_VERTICAL);
        name.setTextColor(Color.BLACK);
        name.setTextSize(19);
        name.setHeight((int) (54 * density));

        Uri contactPhotoUri =
ContentUris.withAppendedId(Contacts.CONTENT_URI, cursor.getLong(0));
        InputStream image_stream =
Contacts.openContactPhotoInputStream(mContent, contactPhotoUri);

        Drawable avatar;
            if (image_stream != null) {
                Bitmap b = BitmapFactory.decodeStream(image_stream);
                avatar = new BitmapDrawable(b);
            } else {
                avatar =
context.getResources().getDrawable(R.drawable.contact_picture);
            }

            avatar.setBounds(0, 0, (int) (54 * density), (int) (54 *
density));
        name.setCompoundDrawablePadding((int) (3 * density));
        name.setCompoundDrawables(avatar, null, null, null);
            return name;
    }

Also the code in bindView is not needed (don't know why there is 2
methods newView and bindView ...)

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // already done in newView
    }

So if you want a autocompletetextView which autocomplete on the name
of the contacts and disply their photo you can use this :

Cursor cursor =
getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
ContactListAdapter.PEOPLE_PROJECTION, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
ContactListAdapter adapter = new ContactListAdapter(this, cursor);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.AutoCompleteTextView);
textView.setAdapter(adapter);

On Nov 18, 11:29 am, Christophe <christophe.lebesner...@gmail.com>
wrote:
> bump
>
> for information, here is the adapter i use for the
> AutocompleteTextView :
>
> import java.io.InputStream;
> import android.content.ContentResolver;
> import android.content.ContentUris;
> import android.content.Context;
> import android.database.Cursor;
> import android.graphics.BitmapFactory;
> import android.net.Uri;
> import android.provider.ContactsContract;
> import android.provider.ContactsContract.Contacts;
> import android.view.LayoutInflater;
> import android.view.View;
> import android.view.ViewGroup;
> import android.widget.CursorAdapter;
> import android.widget.Filterable;
> import android.widget.ImageView;
> import android.widget.TextView;
>
> public class ContactListAdapter extends CursorAdapter implements
> Filterable {
>
>         public static final String[] PEOPLE_PROJECTION = new String[] {
>                 ContactsContract.Contacts._ID,
>         ContactsContract.Contacts.DISPLAY_NAME
>     };
>
>         private ContentResolver mContent;
>
>     public ContactListAdapter(Context context, Cursor c) {
>         super(context, c);
>         mContent = context.getContentResolver();
>     }
>
>     @Override
>     public View newView(Context context, Cursor cursor, ViewGroup
> parent) {
>         View v =
> LayoutInflater.from(context).inflate(R.layout.player_item_autocomplete,
> null);
>         TextView name = (TextView) v.findViewById(R.id.playerName);
>         name.setText(cursor.getString(1));
>
>         Uri contactPhotoUri =
> ContentUris.withAppendedId(Contacts.CONTENT_URI, cursor.getLong(0));
>         InputStream image_stream =
> Contacts.openContactPhotoInputStream(mContent, contactPhotoUri);
>             if (image_stream != null) {
>                 ImageView badge =  (ImageView) 
> v.findViewById(R.id.playerPhoto);
>
> badge.setImageBitmap(BitmapFactory.decodeStream(image_stream));
>             }
>
>         return v;
>     }
>
>     @Override
>     public void bindView(View view, Context context, Cursor cursor) {
>         TextView name = (TextView) view.findViewById(R.id.playerName);
>         name.setText(cursor.getString(1));
>
>         Uri contactPhotoUri =
> ContentUris.withAppendedId(Contacts.CONTENT_URI, cursor.getLong(0));
>         InputStream image_stream =
> Contacts.openContactPhotoInputStream(mContent, contactPhotoUri);
>             if (image_stream != null) {
>                 ImageView badge =  (ImageView)
> view.findViewById(R.id.playerPhoto);
>
> badge.setImageBitmap(BitmapFactory.decodeStream(image_stream));
>             }
>     }
>
>     @Override
>     public String convertToString(Cursor cursor) {
>         return cursor.getString(1);
>     }
>
>     @Override
>     public Cursor runQueryOnBackgroundThread(CharSequence constraint)
> {
>         if (getFilterQueryProvider() != null) {
>             return getFilterQueryProvider().runQuery(constraint);
>         }
>
>         StringBuilder buffer = null;
>         String[] args = null;
>         if (constraint != null) {
>             buffer = new StringBuilder();
>             buffer.append("UPPER(");
>             buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
>             buffer.append(") GLOB ?");
>             args = new String[] { constraint.toString().toUpperCase()
> + "*" };
>         }
>
>         return mContent.query(ContactsContract.Contacts.CONTENT_URI,
> ContactListAdapter.PEOPLE_PROJECTION, buffer == null ? null :
> buffer.toString(), args, ContactsContract.Contacts.DISPLAY_NAME + "
> COLLATE LOCALIZED ASC");
>     }
>
> }
>
> On Nov 17, 5:36 pm, Christophe <christophe.lebesner...@gmail.com>
> wrote:
>
> > Hello,
>
> > I have an AutoCompleteTextView with suggest the name of the people in
> > the contact list. I display a custom view with the photo and the name
> > of the contact in the suggestion. It works perfectly, except nothing
> > happen when I click on one of the suggested item ...
>
> > The view that I use :
>
> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> > android"
> >                     android:layout_height="wrap_content"
> >                     android:minHeight="58dip"
> >                     android:layout_width="fill_parent"
> >                     android:orientation="horizontal"
> >                     android:gravity="center_vertical"
> >                     android:focusable="true"
> >                     
> > android:background="@android:drawable/list_selector_background"
> >                     android:clickable="true">
>
> >     <ImageView android:layout_height="54dip"
> >                 android:layout_marginLeft="2dip"
> >                 android:layout_marginRight="14dip"
> >                 android:layout_width="54dip"
> >                 android:id="@+id/playerPhoto"
> >                 android:src="@drawable/contact_picture"></ImageView>
>
> >     <TextView android:layout_width="fill_parent"
> >                     
> > android:textAppearance="?android:attr/textAppearanceMedium"
> >                     android:maxLines="1"
> >                     android:ellipsize="end"
> >                     android:layout_weight="1"
> >                     android:layout_height="fill_parent"
> >                     android:id="@+id/playerName"
> >                     android:text="name"
> >                     android:layout_gravity="center_vertical"
> >                     android:gravity="center_vertical"
> >                     android:textColor="#000000"
> >                     android:clickable="true"/>
>
> > </LinearLayout>
>
> > ideas ?
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to