我能够获取所有联系人并将其显示在列表视图中。我也得到了联系人的照片。我的问题是,如果联系人没有照片,我的列表视图不会显示任何内容(空格)。我试图检查某些地方,如果ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI有照片或没有,但我找不到某种方法。
在简历中,我想要的是,如果联系人有照片显示照片,如果没有,则显示一个图标。
任何帮助都是合情合理的!
代码:
private final static String[] FROM_COLUMNS = {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI
};
private final static int[] TO_IDS = {
R.id.contactName,
R.id.message_subtitle,
R.id.contact_imageview
};
private static final String[] PROJECTION = {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI
};
private static final String SELECTION = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE ?";
private String[] mSelectionArgs = {"%%"};
private ListView mContactsList;
private SimpleCursorAdapter mCursorAdapter;
public contacts() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.contacts_list_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContactsList = (ListView) getActivity().findViewById(R.id.contacts_list);
mCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item, null, FROM_COLUMNS, TO_IDS, 0);
mContactsList.setOnItemClickListener(this);
mContactsList.setAdapter(mCursorAdapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, SELECTION, mSelectionArgs,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
}
答案 0 :(得分:1)
试试这个:
更改
ContactsContract.CommonDataKinds.Phone.
到
ContactsContract.Contacts.
你的onCreateLoader中的:
return new CursorLoader(getActivity(), baseUri, PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
在适配器中:
@Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView imageView = (ImageView) view.findViewById(R.id.contact_imageview);
Bitmap profileImage = getPhoto(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
if(profileImage !=null) {
imageView.setImageBitmap(profileImage);
}else{
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_contact));
}
}
private Bitmap getPhoto(String id){
Bitmap photo = null;
try{
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(
getActivity().getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI , new Long(id).longValue()));
if(inputStream != null)
photo= BitmapFactory.decodeStream(inputStream);
}catch (Exception e){
}
return photo;
}
}