我无法从联系人那里获取照片并将其放入列表视图中,我将其组织在HashMap中。 当某些联系人没有照片时,我想显示默认照片。 我现在没有收到任何照片。
这是我的代码:
cOznaci = (ImageView) findViewById(R.id.cbOznaci);
ArrayList<HashMap<String, Object>> mapa = new ArrayList<HashMap<String, Object>>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cur.getCount() > 0){
while(cur.moveToNext()){
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
long id2 = Long.parseLong(id);
Object slika = loadContactPhoto(cr, id2);
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){
final Cursor numCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
for(numCur.moveToFirst(); !numCur.isAfterLast(); numCur.moveToNext()){
brTel = numCur.getString(numCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ime = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
tmpIme = new String[] {ime};
for(int i = 0; i < tmpIme.length; i++){
HashMap<String, Object> imeMapa = new HashMap<String, Object>();
imeMapa.put("imeLista", ime);
imeMapa.put("checkBox", slika);
imeMapa.put("Mobilni", brTel);
mapa.add(imeMapa);
}
}
numCur.close();
} // End if
} // While
}
SimpleAdapter sa = new SimpleAdapter(getApplicationContext(), mapa, R.layout.imenik, new String[] {"imeLista", "checkBox", "Mobilni"}, new int[] {R.id.tvImeImenik, R.id.cbOznaci, R.id.tvSamoProba});
lImenik.setAdapter(sa);
}
});
} // onCreate
public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if (input == null) {
return null;
}
return BitmapFactory.decodeStream(input);
}
答案 0 :(得分:0)
您无法通过SimpleAdapter将Bitmaps分配给ImageViews,您只能自动分配字符串和整数。您需要创建ViewBinder
来执行更复杂的分配。
但是,我建议不使用该level-14 API并改为使用PHOTO_URI。这是一个String和SimpleAdapter的默认ViewBinder应该做正确的事情。