代码之家  ›  专栏  ›  技术社区  ›  Parth Sane

如何从列表视图中的onItemClick()获取的列表项的联系人Uri中检索数据?

  •  0
  • Parth Sane  · 技术社区  · 10 年前

    我该怎么做 从列表视图中的uri获取联系人详细信息 ? . 我已经从列表视图中获取了所选联系人的联系人uri。下面的代码片段是我尝试过的,但没有成功。更令人惊讶的是 日志记录语句甚至不执行!

    @Override
    public void onItemClick(
        AdapterView<?> parent, View item, int position, long rowID) {
        // Get the Cursor
        Cursor cursor = ((SimpleCursorAdapter) parent.getAdapter()).getCursor();
        // Move to the selected contact
        cursor.moveToPosition(position);
        // Get the _ID value
        mContactId = cursor.getLong(CONTACT_ID_INDEX);
        // Get the selected LOOKUP KEY
        mContactKey = cursor.getString(CONTACT_KEY_INDEX);
        // Create the contact's content Uri
        mContactUri = Contacts.getLookupUri(mContactId, mContactKey);
        // ListView Clicked item index
    
        String id=new String();
        String name = new String();
        String hasPhone = new String();
        int idx;
    
        Cursor cursor1 = getActivity().getContentResolver().query(mContactUri, null, null, null, null);
        {
            idx = cursor1.getColumnIndex(ContactsContract.Contacts._ID);
            id = cursor1.getString(idx);
    
            idx = cursor1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            name = cursor1.getString(idx);
    
            idx = cursor1.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
            hasPhone = cursor1.getString(idx);
        }
        Log.i("Phone Number","The phone number is: "+hasPhone);
        Log.i("Name","The name is: "+name);
        /*
         * You can use mContactUri as the content URI for retrieving
         * the details for a contact.
         */
    }
    

    这是单击列表视图项目的特定时间的日志。

    09-05 21:55:07.663: I/am_on_resume_called(25140): [0,com.laer.okgo.PersonListActivity]
    09-05 21:55:07.738: W/Adreno-GSL(25140): <ioctl_kgsl_device_getproperty:663>: mmap failed: errno 22 Invalid argument
    09-05 21:55:07.738: I/Adreno-EGL(25140): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I10246dbd022c719c705be805d5642cc8fdfbd2a2Date: 03/10/14
    09-05 21:55:07.739: I/CanvasContext(25140): Initialized EGL, version 1.4
    09-05 21:55:13.932: I/ImageButton(25140): Clicked!!
    

    这是列表视图在由 ImageButton

    09-05 21:55:13.941: I/am_on_paused_called(25140): [0,com.laer.okgo.PersonListActivity]
    

    这是我单击列表项的时候。

    09-05 21:55:17.825: I/am_on_resume_called(25140): [0,com.laer.okgo.PersonListActivity]
    

    此外 奖金问题 。如何更改列表视图的主题/样式?如果有人能给我指明正确的方向就好了!

    1 回复  |  直到 10 年前
        1
  •  0
  •   Parth Sane    10 年前

    这真的是我的错。我单击了错误的UI元素。有两个名称相似的按钮。我没有意识到这一点。哈哈,我搞砸了!浪费了很多时间。。。 因此,故事的寓意是让您的UI/UX元素具有清晰且易于区分的名称!