如何将游标中的值存储到 android 中的字符串中

how to store value from cursor into string in android

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

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

如有错误请指正

试试这个:

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