【问题标题】:Cannot get Phone number of last contact无法获取最后联系人的电话号码
【发布时间】:2013-02-18 06:25:56
【问题描述】:

我一直在遵循this站点的教程,以获取Android联系人的电话号码。

我面临的问题是,我似乎无法获得最后一个联系号码。 例如,如果我保存了 3 个联系人,它会显示前 2 个联系人的号码。

如果对任何人有帮助,我也会发布我的代码。

if(cur.getCount()>0){
        while(cur.moveToNext()){
            String id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))>0){
                pCur=getContentResolver().
                        query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                                ContactsContract.CommonDataKinds.Phone._ID+"=?", 
                                new String[]{id}, null);
                while(pCur.moveToNext()){
                    number.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                }                   
            }
            pCur.close();
        }
    }

任何帮助将不胜感激。 谢谢!

=============================

编辑:以下是对我有用的代码:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext())
    {
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      Log.i("Details", "Name: "+name+"  Number: "+phoneNumber);

    }
    phones.close();

【问题讨论】:

    标签: android android-contacts phone-number


    【解决方案1】:

    您好,请尝试以下代码获取所有联系人

            Cursor c=dbHelper.getAllContact();
            startManagingCursor(c);
            ArrayList<String> columnArray1 = new ArrayList<String>(); 
            if (c.moveToFirst())
            {
                do {          
                    columnArray1.add(c.getString(0));
                } while (c.moveToNext());
            }
    

    【讨论】:

      【解决方案2】:
      String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone._ID,
                                          ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                          ContactsContract.CommonDataKinds.Phone.NUMBER};
          String selection = ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER;
                              + 
          String[] selectionArgs = new String[] { "1" };                            
          Cursor c =  getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                              projection, 
                                              selection, 
                                              selectionArgs, null}
      if (c.moveToFirst())
      {
           do
           {
              Log.d("Test", "Contact: id = " + c.getLong(0) + " name = " + c.getString(1) + " phone = " + c.getString(2));
           }
      }
      

      【讨论】:

      • 感谢您的建议。这似乎对我不起作用。我尝试以不同的方式使用 do-while 循环,但不幸的是我的应用程序强制关闭。这个也一样。
      • 上面的code应该可以得到所有有电话的联系人。
      • 当我使用此代码时,我的应用程序再次关闭。我从其他地方找到了合适的解决方案。无论如何,感谢您的帮助。
      猜你喜欢
      • 2012-08-15
      • 2015-01-24
      • 1970-01-01
      • 2016-12-26
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多