Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.ContentResolver;

import android.database.Cursor;

import android.net.Uri;

import android.provider.ContactsContract;

import android.provider.ContactsContract.CommonDataKinds.StructuredName;

import android.provider.ContactsContract.Data;

public class Main {
    final static public String GetContactNameByID(long contact_id, ContentResolver resolver) {
        if (resolver != null) {
            final Uri uri = Data.CONTENT_URI;

            final String[] columns = { ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                    ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                    ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME
                    //,ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME
                    /*Data._ID,*/
                    //Data.CONTACT_ID, 
                    //Data.LOOKUP_KEY
            };
            final String where = Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                    + StructuredName.CONTENT_ITEM_TYPE + "'";
            final String[] where_arg = { String.valueOf(contact_id) };

            Cursor cur = resolver.query(uri, columns, where, where_arg, null);
            if (cur.moveToFirst()) {
                String result = "";
                final String family_name = cur.getString(0);
                if (family_name != null)
                    result += family_name;
                final String given_name = cur.getString(1);
                if (given_name != null)
                    result += " " + given_name;
                final String middle_name = cur.getString(2);
                if (middle_name != null)
                    result += " " + middle_name;
                //return String.format("%s %s %s",family_name,given_name,middle_name);
                return result;
            } //if(cur.moveToFirst())
        } //if(resolver!=null)
        return null;
    }
}