【问题标题】:Querying all phone numbers of a contact查询联系人的所有电话号码
【发布时间】:2012-03-21 04:35:03
【问题描述】:

我设法编写了一个添加程序,当用户单击一个按钮时,他将能够检索一个电话号码,该电话号码将填充一个 editText 框。问题是,如果一个联系人有多个号码,它总是会占用最高的号码。

我一直在阅读另一个线程,Getting Number from Contacts Picker,有一个答案,但我不明白。由于我是android编程新手, 如果有人可以给我一步一步的指导,我将不胜感激。

【问题讨论】:

    标签: android


    【解决方案1】:

    首先,您可能需要查询电话簿中的所有联系人。

            // Run query
            Uri uri = ContactsContract.Contacts.CONTENT_URI;
            String[] projection = new String[] {
                    ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME
            };
            String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
            String[] selectionArgs = null;
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
    
            // Build adapter with contact entries
            Cursor mCursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
            //
            // Bind mCursor to to your Listview
            //
    

    之后,当用户在您的列表视图中选择一个联系人时,您会进行第二次查询以获取该联系人的标签和号码。

        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            mCursor.moveToPosition(position);
            startManagingCursor(mCursor);       
            String contactID = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
    
            Cursor phoneNumCursor = getContentResolver().query(Phone.CONTENT_URI,  
                    null, Phone.CONTACT_ID + "=?", new String[] { contactID }, null); 
    
            phoneNumCursor.moveToFirst();
    
            Vector<String> phoneTypeList = new Vector<String>();
            Vector<String> phoneNumberList = new Vector<String>();
    
            while (!phoneNumCursor.isAfterLast()){
                int type = phoneNumCursor.getInt(phoneNumCursor.getColumnIndex(Phone.TYPE));
                phoneTypeList.add(String.valueOf(Phone.getTypeLabel(getResources(),type,"")));
                phoneNumberList.add(phoneNumCursor.getString(phoneNumCursor.getColumnIndex(Phone.NUMBER)));
                phoneNumCursor.moveToNext();
            }
            //
            // Feel free to show label and phone number of that contact. ^^
            //
    

    更新:

    如果您想使用联系人选择器,以下是一个示例。

        private static final int CONTACT_PICKER_RESULT = 1001;
    
        protected void startContactPicker(){
            Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);  
            startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 
        }
    
        @Override  
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (resultCode == RESULT_OK) {  
                switch (requestCode) {  
                case CONTACT_PICKER_RESULT:  
                    Cursor cursor = null;  
                    String phoneNumber = "";  
                    try {  
                        Uri result = data.getData();
                        String id = result.getLastPathSegment();
                        cursor = getContentResolver().query(Phone.CONTENT_URI,  
                                null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  
    
                        int phoneIdx = cursor.getColumnIndex(Phone.DATA);  
                        if (cursor.moveToFirst()) {  
                            while (!cursor.isAfterLast()){
                                phoneNumber = cursor.getString(phoneIdx);
                                //
                                // this will go through all phoneNumber of selected contact.
                                //
    
                                cursor.moveToNext();
                            }   
                        }  
                    } catch (Exception e) {
                    } finally {  
                        if (cursor != null) {  
                            cursor.close();  
                        } 
                        numberView.setText(phoneNumber);
    
                    }  
    
                    break;  
                }        
            }
        }
    

    【讨论】:

    • 列表视图的意思,我必须在 XML 中添加?
    • 你可以把它放在 XML 中或通过 java 代码来显示所有联系人姓名的列表。
    • 感谢您的回答!我试着慢慢地通过并理解程序。我目前迷路了
    • 如果你想使用联系人选择器。只是忘记第一个代码块。在方法onActivityResult 中,您只需要这个:String contactID = result.getLastPathSegment(); 来获取所选联系人的ID。然后查询以获取该联系人的类型标签和电话号码。
    • 请查看我的更新答案,并随时尝试一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2011-03-13
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多