我正在尝试仅使用已加星标的联系人显示列表视图,并将图标和名称显示为自定义列表视图。到目前为止,我已经设法在没有Photo的情况下正确显示它们。当我试图包含照片时,我得到了几个错误(因为我尝试了很多不同的接近,这里找到了)。我的最后一次尝试是通过实现Android开发者“* 显示快速联系Badg * e”课程的代码,这里是相关代码:
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
String selection =ContactsContract.Contacts.STARRED + "='1'";
Cursor cursor = managedQuery(queryUri, projection, selection,null,null);
int mIdColumn;
int mLookupKeyColumn;
Uri mContactUri;
mIdColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
// Gets the LOOKUP_KEY index
mLookupKeyColumn = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
mContactUri =
ContactsContract.Contacts.getLookupUri(
cursor.getLong(mIdColumn),
cursor.getString(mLookupKeyColumn)
);
favIcon.assignContactUri(mContactUri);
// The column in which to find the thumbnail ID
int mThumbnailColumn;
/*
* The thumbnail URI, expressed as a String.
* Contacts Provider stores URIs as String values.
*/
String mThumbnailUri;
/*
* Gets the photo thumbnail column index if
* platform version >= Honeycomb
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mThumbnailColumn =
cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI);
// Otherwise, sets the thumbnail column to the _ID column
} else {
mThumbnailColumn = mIdColumn;
}
/*
* Assuming the current Cursor position is the contact you want,
* gets the thumbnail ID
*/
mThumbnailUri = cursor.getString(mThumbnailColumn);
Bitmap mThumbnail =
loadContactPhotoThumbnail(mThumbnailUri);
favIcon.setImageBitmap(mThumbnail);
String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
int to[] = new int[]{
R.id.ivDefContact,
R.id.tvContactName
};
ListAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.favs_list_item,
cursor,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
final ListView listStarred = (ListView) findViewById(R.id.lvFavs);
listStarred.setAdapter(adapter);
}
我使用上面代码得到的错误是:
Android.database.CursorIndexOutOfBoundsException:请求索引-1, 大小为9 (9是我的已加星标的联系人的数量),并将我指向第85行,即:
mContactUri =
ContactsContract.Contacts.getLookupUri(
cursor.getLong(mIdColumn),
cursor.getString(mLookupKeyColumn)
);
如果我评论 R.id.ivDefContact,行,则列表视图的活动运行正常,并显示正确的联系人姓名。所以问题在于Photo实现。我读了一些相关的线程,但我不知道它是如何工作的。
编辑: Logcat错误:
java.lang.RuntimeException:无法启动活动ComponentInfo android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为6 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357) 在android.app.ActivityThread.access $ 600(ActivityThread.java:153) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1247) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5226) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) at dalvik.system.NativeStart.main(Native Method) 引起:android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为6 在android.database.AbstractCursor.checkPosition(AbstractCursor.java:424) 在android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 在android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74) 在android.database.CursorWrapper.getLong(CursorWrapper.java:106) 在com.example.DialerActivity.onCreate(DialerActivity.java:85) 在android.app.Activity.performCreate(Activity.java:5104) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) ......还有11个
答案 0 :(得分:2)
问题在这里
String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
int to[] = new int[]{
R.id.ivDefContact,
R.id.tvContactName
};
两者(来自和来自)的项目数必须相同。
因此,您可以将from
重写为
String[] from = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME};
答案 1 :(得分:1)
我设法显示上述更改的照片:
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED};
String selection =ContactsContract.Contacts.STARRED + "='1'";
Cursor cursor = managedQuery(queryUri, projection, selection,null,null);
long id= cursor.getColumnIndex(ContactsContract.Contacts._ID);
Bitmap bitmap = loadContactPhoto(getContentResolver(), id);
if(bitmap!=null){
favIcon.setImageBitmap(bitmap);
}
else{
}
String[] from = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME};
int to[] = new int[]{
R.id.ivDefContact,
R.id.tvContactName
};
ListAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.favs_list_item,
cursor,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
final ListView listStarred = (ListView) findViewById(R.id.lvFavs);
listStarred.setAdapter(adapter);
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);
}
现在照片显示的是带照片的联系人的照片。