在android中检索带有号码的联系人列表

发布于 2024-11-17 22:45:40 字数 843 浏览 3 评论 0原文

您好,我想从 android 中的联系人列表中检索姓名和号码,我使用以下代码,它会给我姓名,但不会给我号码,它会为号码提供空值

注意:我正在使用 android 模拟器

  public void demo(){
    String phoneNumber;
    String [] item = new String[]{ People._ID,

            People.NAME,
            People.NUMBER};

    Cursor cur = getContentResolver().query(People.CONTENT_URI ,item , null, null, null);
    int phoneNumberIndex = cur.getColumnIndexOrThrow(People.NUMBER);
    if (cur != null) {
        Log.v("cur not null", "Cursor Not null");

        if (cur.moveToNext()) {
            Log.v("moveToNext", "Moved to first");
            Log.v("moveToNext", "Cursor Moved to first and checking");
            phoneNumber = cur.getString(phoneNumberIndex);
            System.out.println("****** from_number "+phoneNumber +" *****************");
        }
    }
}

Hi i want to retrieve name and numbers from contact list in android ,am using following code, it will give me name but not number it gives null value for number

NOTE: am using android emulator

  public void demo(){
    String phoneNumber;
    String [] item = new String[]{ People._ID,

            People.NAME,
            People.NUMBER};

    Cursor cur = getContentResolver().query(People.CONTENT_URI ,item , null, null, null);
    int phoneNumberIndex = cur.getColumnIndexOrThrow(People.NUMBER);
    if (cur != null) {
        Log.v("cur not null", "Cursor Not null");

        if (cur.moveToNext()) {
            Log.v("moveToNext", "Moved to first");
            Log.v("moveToNext", "Cursor Moved to first and checking");
            phoneNumber = cur.getString(phoneNumberIndex);
            System.out.println("****** from_number "+phoneNumber +" *****************");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌吟 2024-11-24 22:45:40

它给出 null 因为你试图只检索一个数字..这应该有效..

if (cur.moveToNext()) {
            Log.v("moveToNext", "Moved to first");
            Log.v("moveToNext", "Cursor Moved to first and checking");
while(cur.moveToNext())
{
            phoneNumber = cur.getString(phoneNumberIndex);
            System.out.println("****** from_number "+phoneNumber +" *****************");
}
        }

It gives null because u r trying to retrieve only one number.. this should work..

if (cur.moveToNext()) {
            Log.v("moveToNext", "Moved to first");
            Log.v("moveToNext", "Cursor Moved to first and checking");
while(cur.moveToNext())
{
            phoneNumber = cur.getString(phoneNumberIndex);
            System.out.println("****** from_number "+phoneNumber +" *****************");
}
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文