Example usage for android.database Cursor getColumnIndex

List of usage examples for android.database Cursor getColumnIndex

Introduction

In this page you can find the example usage for android.database Cursor getColumnIndex.

Prototype

int getColumnIndex(String columnName);

Source Link

Document

Returns the zero-based index for the given column name, or -1 if the column doesn't exist.

Usage

From source file:Main.java

/**
 * Gets a contact number and then displays the name of contact in the DP
 *
 * @param context       context of the activity from which it was called.
 * @param contactNumber a string representation of the contact number
 * @return returns the number if no contact exist.
 *//*from www . j  a  v  a 2s.  c  o  m*/
public static String getContactName(Context context, String contactNumber) {
    String displayName = null;
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));

    Cursor cur = context.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

    if (cur != null && cur.moveToFirst()) {
        displayName = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    } else {
        displayName = contactNumber;
    }

    if (!cur.isClosed()) {
        cur.close();
    }

    return displayName;
}

From source file:Main.java

/**
 * Gets the Uri to a specific audio file
 * @param filePath The path of the file that we are looking up
 * @param contentResolver The content resolver that is used to perform the query
 * @return The Uri of the sound file//w  w  w . j  a  va2 s.co m
 */
private static Uri getAudioUriFromFilePath(String filePath, ContentResolver contentResolver) {
    long audioId;
    Uri uri = MediaStore.Audio.Media.getContentUri("external");
    String[] projection = { BaseColumns._ID };
    Cursor cursor = contentResolver.query(uri, projection, MediaStore.MediaColumns.DATA + " LIKE ?",
            new String[] { filePath }, null);

    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(projection[0]);
        audioId = cursor.getLong(columnIndex);
        cursor.close();
        return Uri.parse(uri.toString() + "/" + audioId);
    }
    return null;
}

From source file:Main.java

public static String getPath(Context context, Uri uri) {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor;

        try {//from  ww w.ja va2s.  c  om
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndex("_data");
            if (column_index != -1 && cursor.moveToFirst()) {
                String path = cursor.getString(column_index);
                if (path == null) {
                    path = getNewTemporaryFilePath(context, uri);
                }
                return path;
            } else {
                return getNewTemporaryFilePath(context, uri);
            }
        } catch (Exception e) {
            return getNewTemporaryFilePath(context, uri);
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

/**
 * Tries to find out if the given cursor points to a dataset with unread articles in it, returns true if it does.
 *
 * @param cursor the cursor.//from w w  w.j  a  va2 s.  c o m
 * @return true if there are unread articles in the dataset, else false.
 */
private static boolean checkUnread(Cursor cursor) {
    if (cursor == null || cursor.isClosed())
        return false; // Check null or closed

    if (!cursor.moveToFirst())
        return false; // Check empty

    do {
        if (cursor.getInt(cursor.getColumnIndex("unread")) > 0)
            return cursor.moveToFirst(); // One unread article found, move to first entry
    } while (cursor.moveToNext());

    cursor.moveToFirst();
    return false;
}

From source file:Main.java

/**
 * method used to lookup the id of a contact photo id
 *
 * @param context/*from  w w  w  .  j  a  v a2  s .  c  om*/
 *            a context object used to get a content resolver
 * @param phoneNumber
 *            the phone number of the contact
 *
 * @return the id of the contact
 */
public static long lookupPhotoId(Context context, String phoneNumber) {

    long mPhotoId = -1;

    Uri mLookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

    String[] mProjection = new String[2];
    mProjection[0] = PhoneLookup._ID;
    mProjection[1] = PhoneLookup.PHOTO_ID;

    Cursor mCursor = context.getContentResolver().query(mLookupUri, mProjection, null, null, null);

    if (mCursor.getCount() > 0) {
        mCursor.moveToFirst();

        mPhotoId = mCursor.getLong(mCursor.getColumnIndex(PhoneLookup._ID));

        mCursor.close();
    }

    return mPhotoId;
}

From source file:edu.mit.mobile.android.locast.data.interfaces.LocatableUtils.java

/**
 * Get the latitude/longitude from the row currently selected in the cursor. Requires
 * LocatableUtils.Locatable._LATITUDE and LocatableUtils.Locatable._LONGITUDE to be selected.
 *
 * @param c/*from w w  w .  j a  v a 2 s  .  c o m*/
 * @return
 */
public static Location toLocation(Cursor c) {
    final int lat_idx = c.getColumnIndex(Locatable.COL_LATITUDE);
    final int lon_idx = c.getColumnIndex(Locatable.COL_LONGITUDE);
    if (c.isNull(lat_idx) || c.isNull(lon_idx)) {
        return null;
    }
    final Location l = new Location("internal");
    l.setLatitude(c.getDouble(lat_idx));
    l.setLongitude(c.getDouble(lon_idx));
    return l;
}

From source file:Main.java

/**
 * 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.//  w w w  . j a  v  a 2 s  .c  om
 */
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 "";
}

From source file:Main.java

public static String getRealFilePath(Context context, Uri uri) {
    if (null == uri)
        return null;
    String scheme = uri.getScheme();
    String data = null;/*w  w w. ja v a2s  .  c o m*/
    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
                null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

public static String getApnPort(Context context) {
    Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
    c.moveToFirst();/*from ww w . j a  v  a2  s  .  c  o  m*/
    if (c.isAfterLast()) {
        c.close();
        return "80";
    }

    String port = null;
    port = c.getString(c.getColumnIndex(APN_PROP_PORT));
    if (port == null) {
        c.close();
        port = "80";
    }
    c.close();
    return port;
}

From source file:Main.java

public static String getMessageCount(Context context, String id) {
    String res = null;/*from  w  ww  . java2 s .c o m*/
    try {
        final String[] projection = new String[] { "_id", "message_count" };
        Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
        Cursor query = context.getContentResolver().query(uri, projection, null, null, "date DESC");
        if (query != null) {
            boolean find = false;
            while (query.moveToNext() && !find) {
                if (query.getString(query.getColumnIndex("_id")).equals(id)) {
                    res = query.getString(query.getColumnIndex("message_count"));
                    //                        Log.v("getMessageCount", "find, nb_sms = "+res);
                    find = true;
                }
            }
            query.close();
        }
    } catch (Exception e) {
        //            Log.v("getMessageCount", "Erreur");
        e.printStackTrace();
    }
    return res;
}