1

アイコンと名前をカスタムリストビューに付けて、スター付きの連絡先のみを含むリストビューを表示しようとしています。これまでのところ、写真なしで正しく表示することができました。写真を含めようとすると、いくつかのエラーが発生します(ここで見つかったさまざまなアプローチを試したためです)。私の最後の試みは、Android Developers の「*クイック コンタクト バッジの表示*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にコメントを付けると、Activity にリストビューが正常に実行され、連絡先の名前が正しく表示されます。したがって、問題は Photo の実装にあります。関連するスレッドをいくつか読みましたが、それがどのように機能するかわかりません。

編集: Logcat エラー:

java.lang.RuntimeException: アクティビティ ComponentInfo android.database.CursorIndexOutOfBoundsException を開始できません: android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) で android.app.ActivityThread.handleLaunchActivity でサイズ 6 のインデックス -1 が要求されました(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) で java.lang.reflect.Method.invokeNative(Native Method) でcom.android.internal.os の java.lang.reflect.Method.invoke(Method.java:511)。ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) at dalvik.system.NativeStart.main(Native Method) 原因: android.database. CursorIndexOutOfBoundsException: android.database.AbstractWindowedCursor.getLong で android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) で android.database.AbstractCursor.checkPosition(AbstractCursor.java:424) で 6 のサイズでインデックス -1 が要求されました(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) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) ... 11 詳細

4

2 に答える 2

2

問題はこちら

String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
    int to[] = new int[]{
            R.id.ivDefContact,
            R.id.tvContactName
    }; 

両方 (from と to) の項目数は同じでなければなりません。

fromしたがって、次のように書き換えることができます

String[] from = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME};
于 2013-05-31T08:20:44.340 に答える
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);
}

写真がある連絡先の写真が正しく表示されるようになりました。

于 2013-05-31T13:13:30.240 に答える