【问题标题】:how to store value from cursor into string in android如何将光标中的值存储到android中的字符串中
【发布时间】:2015-07-16 12:58:23
【问题描述】:

我正在创建可以从号码中检索联系人姓名的应用程序。在谷歌搜索时,我从这篇文章中得到了这段代码: Getting contact name from number in Android 2.3.4

public static String getContactName(String num, ContentResolver cr) {

    Uri u = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI Uri.encode(num));
    String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME};

    Cursor c = cr.query(u, projection, null, null, null);

    try {
        if (!c.moveToFirst())
            return number;

        int index = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        return c.getString(index);

    } finally {
        if (c != null)
            c.close();
    }
}

我可以在函数中成功将 Number 作为 String num 发送, 但不知道如何将联系人姓名存储到字符串中。我不熟悉 Android 中的 Cursor

如果我错了,请纠正我。

【问题讨论】:

  • 你已经通过返回一个字符串来完成它。所以现在你想做什么?你的问题很不清楚。

标签: android android-intent android-contacts android-cursor


【解决方案1】:

试试这个:

String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

【讨论】:

  • 您是否在清单中设置了读取联系人的权限? -->
  • 是的,我已经添加了权限,请提出其他解决方案,谢谢@jlopez
  • 我在“ if(cursor.moveToFirst()) { } ”中编写了这段代码,现在它正在工作@jlopez
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 2021-09-19
  • 2011-09-03
相关资源
最近更新 更多