Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;

import android.provider.ContactsContract.Data;

public class Main {
    public static final Uri PHONE_CONTACTS_URI = Uri.parse("content://com.android.contacts/data");

    public static String getFirstCNLetterByContactId(Context context, long contactId) {
        String result = "";
        String[] projection = {
                // Data._ID,
                // Data.DISPLAY_NAME,
                // Data.CONTACT_ID,
                Data.DATA12, };
        String where = Data.CONTACT_ID + "=?";
        final String sortOrder = null;
        Cursor cursor = context.getContentResolver().query(PHONE_CONTACTS_URI, projection, where,
                new String[] { String.valueOf(contactId) }, sortOrder);

        if (cursor != null) {
            try {
                if (cursor.getCount() > 0) {
                    while (cursor.moveToNext()) {
                        String nameLetters = cursor.getString(cursor.getColumnIndexOrThrow("data12"));
                        if (null != nameLetters && !"".equals(nameLetters)) {
                            result = nameLetters.substring(0, 1).toUpperCase();
                            break;
                        }
                    }
                }

            } finally {
                cursor.close();
            }
        }

        return result;
    }
}