如何在游戏中将光标值存储到字符串中

时间:2021-02-25 13:34:07

I'm creating application that can retrieve contact name from number. While Googling, I got this block of code from this post: Getting contact name from number in Android 2.3.4

我正在创建可以从号码中检索联系人姓名的应用程序。谷歌搜索时,我从这篇文章中得到了这段代码:从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();
    }
}

I'm successfully able to send Number as String num in the function, but do not know how to store contact name into String. I'm not familiar with Cursor in Android

我已成功地在函数中发送Number as String num,但不知道如何将联系人名称存储到String中。我不熟悉Android中的Cursor

If I'm wrong please correct me.

如果我错了请纠正我。

1 个解决方案

#1


Try this:

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

#1


Try this:

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