Android:在ListView中设置联系人照片,其中包含姓名和号码

时间:2011-02-14 17:43:40

标签: android listview contacts photo image

我正在创建一个应用程序,需要显示带有照片,姓名和号码的手机通讯录。我创建了一个ListView,可以用通用照片显示联系人和数字,但我无法显示contat的图片。这是我的代码:

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);
}

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); }

如何在SimpleCursorAdapter中附加联系人的图片?还有另一个吗?

非常感谢。

2 个答案:

答案 0 :(得分:2)

我明白了。如果有人遇到同样的问题,我就这样做了:

在扩展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;
}

在自定义适配器中:

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);
  }
}

答案 1 :(得分:0)

我知道这是一个非常古老的问题,但答案也是如此,因为此解决方案中的一些内容现在已被弃用。当我在寻找类似的解决方案时,问题出现在搜索中,但我会在这里加上我的两分钱......

我创建了一个简单的联系人列表,其中包含ContactsContract中的姓名和照片。 请在... https://stackoverflow.com/a/37710199/1209544

查看我的回答