Example usage for android.database Cursor getString

List of usage examples for android.database Cursor getString

Introduction

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

Prototype

String getString(int columnIndex);

Source Link

Document

Returns the value of the requested column as a String.

Usage

From source file:Main.java

public static ArrayList<HashMap<String, String>> cursorToHashMap(Cursor cursor) {

    if (cursor != null) {
        int cursorCount = cursor.getCount();
        int columnCount;
        ArrayList<HashMap<String, String>> cursorData = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> rowHashMap;
        for (int i = 0; i < cursorCount; i++) {
            cursor.moveToPosition(i);//  ww  w .j a v  a 2 s  .c  om
            rowHashMap = new HashMap<String, String>();
            columnCount = cursor.getColumnCount();
            for (int j = 0; j < columnCount; j++) {
                rowHashMap.put(cursor.getColumnName(j), cursor.getString(j));
            }
            cursorData.add(rowHashMap);
        }
        cursor.close();

        return cursorData;
    } else {
        return null;
    }
}

From source file:com.deliciousdroid.platform.ContactManager.java

private static String lookupUsername(ContentResolver resolver, long id) {
    String result = null;/*  w  w w  .j  ava2  s  .c om*/
    final Cursor c = resolver.query(RawContacts.CONTENT_URI, UsernameQuery.PROJECTION, UsernameQuery.SELECTION,
            new String[] { Long.toString(id) }, null);
    try {
        while (c.moveToNext()) {
            result = c.getString(UsernameQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return result;
}

From source file:com.chen.emailcommon.utility.AttachmentUtilities.java

/**
 * In support of deleting a message, find all attachments and delete associated cached
 * attachment files.//ww  w.  jav a 2s.c  om
 * @param context
 * @param accountId the account for the message
 * @param messageId the message
 */
public static void deleteAllCachedAttachmentFiles(Context context, long accountId, long messageId) {
    final Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, messageId);
    final Cursor c = context.getContentResolver().query(uri, ATTACHMENT_CACHED_FILE_PROJECTION, null, null,
            null);
    try {
        while (c.moveToNext()) {
            final String fileName = c.getString(0);
            if (!TextUtils.isEmpty(fileName)) {
                final File cachedFile = new File(fileName);
                // Note, delete() throws no exceptions for basic FS errors (e.g. file not found)
                // it just returns false, which we ignore, and proceed to the next file.
                // This entire loop is best-effort only.
                cachedFile.delete();
            }
        }
    } finally {
        c.close();
    }
}

From source file:net.kourlas.voipms_sms.Utils.java

/**
 * Gets a URI pointing to a contact's photo, given the URI for that contact.
 *
 * @param applicationContext The application context.
 * @param uri                The URI of the contact.
 * @return a URI pointing to the contact's photo.
 *//*from   w  w w . j a v a2  s.co m*/
public static String getContactPhotoUri(Context applicationContext, Uri uri) {
    Cursor cursor = applicationContext.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI },
            null, null, null);
    if (cursor.moveToFirst()) {
        String photoUri = cursor
                .getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
        cursor.close();
        return photoUri;
    } else {
        cursor.close();
        return null;
    }
}

From source file:Main.java

/**
 * Query the server app to get the file's display name.
 *//*  w  w  w . j  ava  2 s  .c o  m*/
public static String getUriFileName(Context context, Uri uri) {
    if (uri == null) {
        return null;
    }

    if (uri.getScheme().equals("file")) {
        return uri.getLastPathSegment();
    }

    if (uri.getScheme().equals("content")) {
        Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);

        //Get the column index of the data in the Cursor,
        //move to the first row in the Cursor, get the data, and display it.
        int name_index = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        //int size_index = returnCursor.getColumnIndex(OpenableColumns.SIZE);

        if (name_index < 0) {
            return null;
        }

        returnCursor.moveToFirst();

        //return returnCursor.getLong(size_index)
        return returnCursor.getString(name_index);
    }
    return null;
}

From source file:Main.java

private static String dumpRow(Cursor c) {
    String result = "VALUES(";

    for (int pos = 0; pos < c.getColumnCount(); pos++) {
        switch (c.getType(pos)) {
        case Cursor.FIELD_TYPE_NULL:
            result += "null";
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            result += Integer.toString(c.getInt(pos));
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            result += Float.toString(c.getFloat(pos));
            break;
        case Cursor.FIELD_TYPE_STRING:
            result += DatabaseUtils.sqlEscapeString(c.getString(pos));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            result += "X'";
            for (byte b : c.getBlob(pos)) {
                result += String.format("%02X", b);
            }//from   w w  w .  ja v a2 s. co  m
            result += "'";
            break;
        default:
            return "Invalid field type " + c.getType(pos) + " at position " + pos;
        }

        if (pos < c.getColumnCount() - 1) {
            result += ", ";
        }
    }
    return result + ")";
}

From source file:com.android.browser.BookmarksPageCallbacks.java

static String getUrl(Cursor c) {
    return c.getString(BookmarksLoader.COLUMN_INDEX_URL);
}

From source file:im.delight.android.baselib.Social.java

/**
 * Returns a list of phone numbers for the contact with the given lookup ID
 *
 * @param lookupID the lookup ID to get the phone numbers for
 * @param context Context instance to get the ContentResolver from
 * @return CharSequence[] containing all phone numbers for the given contact
 *///from  w ww . j a  v  a 2 s  .  c om
public static CharSequence[] getContactPhone(String lookupID, Context context) {
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
    String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    String[] selectionArgs = new String[] { lookupID };
    String sortOrder = null;
    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);
    String phone;
    if (result != null) {
        if (result.getCount() > 0) {
            CharSequence[] res = new CharSequence[result.getCount()];
            int i = 0;
            while (result.moveToNext()) {
                phone = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                if (phone != null) {
                    res[i] = phone;
                    i++;
                }
            }
            result.close();
            return res;
        } else {
            result.close();
            return null;
        }
    } else {
        return null;
    }
}

From source file:im.delight.android.baselib.Social.java

/**
 * Returns a list of email addresses for the contact with the given lookup ID
 *
 * @param lookupID the lookup ID to get the email addresses for
 * @param context Context instance to get the ContentResolver from
 * @return CharSequence[] containing all email addresses for the given contact
 *//*  ww  w  .j  a v a 2  s  .c om*/
public static CharSequence[] getContactEmail(String lookupID, Context context) {
    Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Email.DATA };
    String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    String[] selectionArgs = new String[] { lookupID };
    String sortOrder = null;
    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);
    String email;
    if (result != null) {
        if (result.getCount() > 0) {
            CharSequence[] res = new CharSequence[result.getCount()];
            int i = 0;
            while (result.moveToNext()) {
                email = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                if (email != null) {
                    res[i] = email;
                    i++;
                }
            }
            result.close();
            return res;
        } else {
            result.close();
            return null;
        }
    } else {
        return null;
    }
}

From source file:at.diamonddogs.util.Utils.java

/**
 * Creates a {@link String} {@link List} from a {@link Cursor}
 *
 * @param cursor the input {@link Cursor}
 * @param name   the name of the colum/*from ww w. j a v a  2 s.  c om*/
 * @return a {@link String} {@link List}
 */
public static List<String> convertColumnToList(Cursor cursor, String name) {
    List<String> list = new ArrayList<>();
    if (!checkCursor(cursor)) {
        return null;
    }
    cursor.moveToFirst();
    do {
        list.add(cursor.getString(cursor.getColumnIndex(name)));
    } while (cursor.moveToNext());

    return list;
}