get Contact Display Name from Id - Android Account

Android examples for Account:Contact Name

Description

get Contact Display Name from Id

Demo Code

/**//from www.j a v  a  2 s  .  c  om
 * Copyright (C) 2014
 * Bearstouch Software : <mail@bearstouch.com>
 *
 * This file is part of Bearstouch Android Lib.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;

import android.net.Uri;

import android.provider.ContactsContract;

public class Main {
    /**
     * @param ctx
     * @param contactId
     * @return
     */
    public static String getDisplayNamefromId(Context ctx, long contactId) {

        Uri cUri = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI, contactId);

        Cursor cur = ctx.getContentResolver().query(cUri, null, null, null,
                null);

        if (cur.moveToFirst()) {
            int DisplayIndex = cur
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            String DisplayName = cur.getString(DisplayIndex);
            return DisplayName;
        }

        return "";
    }
}

Related Tutorials