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 getFirstCNLetterByContactId8(Context context, long contactId) {
        String result = "";
        String[] projection = { "_id", "display_name", "data1", "sort_key" };
        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("sort_key"));
                        if (null != nameLetters && !"".equals(nameLetters)) {
                            result = nameLetters.substring(0, 1).toUpperCase();
                            break;
                        }
                    }
                }

            } finally {
                cursor.close();
            }
        }

        return result;
    }
}