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