Android: set contact photo in ListView with name and number

I am creating an application that should display the phone’s contacts with its photo, name and number. I created a ListView and can show contacts and numbers with a common photo, but I can not show the contat image. This is my code:

 Cursor cursor = getContacts(); String[] fields = new String[] {Contacts.DISPLAY_NAME, Phone.NUMBER}; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor, fields, new int[] {R.id.contactEntryText, R.id.contactEntryNumber}); mContactList.setAdapter(adapter); private Cursor getContacts() // Run query Uri uri = Phone.CONTENT_URI; String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Phone.NUMBER, Contacts.PHOTO_ID }; //String selection = Contacts.HAS_PHONE_NUMBER + "='1'"; String selection = null; String[] selectionArgs = null; String sortOrder = Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; return managedQuery(uri, projection, selection, selectionArgs, sortOrder); } 

code>

How can I attach a contact image in SimpleCursorAdapter? Is there some more?

Thank you very much.

+3
source share
2 answers

I get it. If someone had the same problem, here is how I did it:

In an activity that extends ListActivity:

  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Cursor names = getNamesAndPictures(); names.moveToFirst(); ListAdapter adapter = new MySimpleCursorAdapter(this, R.layout.contacts, names, new String[] {Phones.NAME, Phones.NUMBER}, new int[] { R.id.Nombre, R.id.Numero}); setListAdapter(adapter); startManagingCursor(names); Log.i(DEBUG_TAG, "Mi numero: " + miNumeroTelefono()); } public Cursor getNamesAndPictures(){ String[] projection = new String[] { Phones.NUMBER, Phones.PERSON_ID, People.NAME, People._ID }; String selection = Phones.NAME + "!='null'"; String sortOrder = Phones.NAME + " COLLATE LOCALIZED ASC"; Cursor cursor = managedQuery(Phones.CONTENT_URI, projection, selection, null, sortOrder); return cursor; } 

In user adapter:

 public class MySimpleCursorAdapter extends SimpleCursorAdapter { ... @Override public void bindView(View view, Context context, Cursor cursor) { ImageView imageView = (ImageView) view.findViewById(R.id.Foto); int id = cursor.getColumnIndex(Phones.PERSON_ID); Uri uri = ContentUris.withAppendedId(Phones.CONTENT_URI, cursor.getLong(id)); String uriPhoto = uri.toString(); String uriPeople = uriPhoto.replace("phones", "people"); Uri uriFinal = Uri.parse(uriPeople); Bitmap bitmap = People.loadContactPhoto(context, uriFinal, R.drawable.avatar02, null); imageView.setImageBitmap(bitmap); super.bindView(view, context, cursor); } } 
+2
source

I know that this is a very old question, but that’s the answer, since there is little left in this decision. Since the question appeared in the search when I was looking for a similar solution, I though I will add my two cents here ...

I created a simple contact list with their names and photos with ContactsContract . Please check my answer ... fooobar.com/questions/173099 / ...

0
source

Source: https://habr.com/ru/post/1343680/


All Articles