【问题标题】:Search Contact By Name按姓名搜索联系人
【发布时间】:2013-09-07 04:46:01
【问题描述】:

我正在尝试创建一个自定义联系人应用程序,该应用程序仅显示那些具有联系人号码联系人。首先,有没有自动化的方法来做到这一点?假设不是,那么我正在尝试按名称搜索联系人,例如罗汉

这里是代码:-

Cursor photoCursor = getContentResolver().query(
            android.provider.ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.Contacts.DISPLAY_NAME },
            ContactsContract.Contacts.DISPLAY_NAME + " = ?",
            new String[]{"Rohan"}, null);
    photoCursor.moveToFirst();
    while (photoCursor.moveToNext()) {
        Log.d("Photo Thumbnail", "" + photoCursor.getString(1));
    }

虽然联系人存在,但我没有收到任何日志,如果我删除 SelectionSelection Args,我会在日志中看到 Rohan。我做错了什么?

【问题讨论】:

    标签: android android-contacts android-cursor android-search


    【解决方案1】:

    搜索部分显示名称的简单解决方案。

    ContentResolver contentResolver = getCurrentActivity().getContentResolver();
    
    String   whereString = "display_name LIKE ?";
    String[] whereParams = new String[]{ "%" + searchText + "%" };
    
    Cursor contactCursor = contentResolver.query(
            ContactsContract.Data.CONTENT_URI,
            null,
            whereString,
            whereParams,
            null );
    
    while( contactCursor.moveToNext() ) {
    
        int contactId = getIntFromCursor( contactCursor, ContactsContract.Data.CONTACT_ID );
    
        Log.d( "Contact ID", contactId)
    
    }
    
    contactCursor.close();
    

    【讨论】:

      【解决方案2】:

      我是用下面的代码做到的

      Cursor cursor = getContentResolver().query(
                  android.provider.ContactsContract.Contacts.CONTENT_URI,
                  new String[] { ContactsContract.Contacts.PHOTO_ID,
                          ContactsContract.Contacts.DISPLAY_NAME,
                          ContactsContract.Contacts._ID },
                  ContactsContract.Contacts.HAS_PHONE_NUMBER, null,
                  ContactsContract.Contacts.DISPLAY_NAME);
      

      这个cursor 提供了所有具有任何电话号码的联系人,然后我将唯一的ID 保存在ArrayList 中,就像这样

      cursor.moveToFirst();
      
          while (cursor.moveToNext()) {
              contactsID.add(cursor.getString(2));
          }
      

      然后在选择联系人时,我使用此查找联系人号码

      Cursor cursor = getContentResolver()
                          .query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                  new String[] {
                                          ContactsContract.CommonDataKinds.Phone.NUMBER,
                                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                                          ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
                                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                          + " = ?",
                                  new String[] { contactsID.get(position) }, null);
                  contactNumbers = new ArrayList<String>();
                  while (cursor.moveToNext()) {
                      contactNumbers.add(cursor.getString(0));
                      Log.d("number", cursor.getString(0));
                  }
      

      【讨论】:

        【解决方案3】:

        试试这个:

            Cursor contactLookupCursor =
                    getContentResolver().query(
                            Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                                    Uri.encode("Rohan")),
                            new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER},
                            null,
                            null,
                            null);
        
            try {
                while (contactLookupCursor.moveToNext()) {
                    contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
                    contactNumber = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.NUMBER));
                }
            } finally {
                contactLookupCursor.close();
            }
        

        【讨论】:

        • 我在 while 循环中放了一个 Log.d,打印 contactNamecontactNumber 但没有收到任何输出。
        • 搜索名称时不产生任何内容
        【解决方案4】:

        您似乎正在尝试实现一个允许用户选择联系人的屏幕,然后选择该联系人的电话号码。

        如果是这种情况,您可以改用电话选择器意图:

        Intent intent = Intent(Intent.ACTION_PICK);
        intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
        

        这将打开本机联系人应用程序,并允许用户选择联系人和电话号码。 然后,您将在您的应用中收到如下结果:

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
                // Get the URI and query the content provider for the phone number
                Uri contactUri = data.getData();
                String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
                Cursor cursor = getContentResolver().query(contactUri, projection,
                        null, null, null);
                // If the cursor returned is valid, get the phone number
                if (cursor != null && cursor.moveToFirst()) {
                    int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
                    String number = cursor.getString(numberIndex);
                    // Do something with the phone number
                    ...
                }
            }
        }
        

        【讨论】:

        • 问题太老了,所以我不记得了,但当时我想我想显示所有附有号码的联系人。虽然这个答案会帮助想要选择联系人的人。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-21
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多