Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Use of this source code is governed by a BSD-style license that can be

import android.content.ContentResolver;

import android.database.Cursor;
import android.net.Uri;

public class Main {
    /**
     * Method to resolve the display name of a content URI.
     *
     * @param uri the content URI to be resolved.
     * @param contentResolver the content resolver to query.
     * @param columnField the column field to query.
     * @return the display name of the @code uri if present in the database
     *  or an empty string otherwise.
     */
    public static String getDisplayName(Uri uri, ContentResolver contentResolver, String columnField) {
        if (contentResolver == null || uri == null)
            return "";
        Cursor cursor = null;
        try {
            cursor = contentResolver.query(uri, null, null, null, null);

            if (cursor != null && cursor.getCount() >= 1) {
                cursor.moveToFirst();
                int index = cursor.getColumnIndex(columnField);
                if (index > -1)
                    return cursor.getString(index);
            }
        } catch (NullPointerException e) {
            // Some android models don't handle the provider call correctly.
            // see crbug.com/345393
            return "";
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return "";
    }
}