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:com.android.browser.BookmarksPageCallbacks.java

static Intent createShortcutIntent(Context context, Cursor cursor) {
    String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL);
    String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
    Bitmap touchIcon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_TOUCH_ICON);
    Bitmap favicon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON);
    return BookmarkUtils.createAddToHomeIntent(context, url, title, touchIcon, favicon);
}

From source file:Main.java

public static LinkedHashMap<String, Uri> GetMatchedContactsList(Context context, String searchTerm) {
    LinkedHashMap<String, Uri> contactList = new LinkedHashMap<String, Uri>();
    ContentResolver cr = context.getContentResolver();
    String columns[] = { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
    Cursor cur;
    if (searchTerm == null) {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
    } else {//from www.  j  ava  2  s  .  c  o  m
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns,
                ContactsContract.Contacts.DISPLAY_NAME + " LIKE "
                        + DatabaseUtils.sqlEscapeString("%" + searchTerm + "%"),
                null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    }
    if (cur != null) {
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String photoURI = cur
                        .getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                if (photoURI != null) {
                    Uri thumbUri = Uri.parse(photoURI);
                    contactList.put(name, thumbUri);
                }
            }
        }
        cur.close();
    }
    return contactList;
}

From source file:Main.java

static Object getObject(Cursor cursor, int columnIndex) throws Exception {
    int type = getType(cursor, columnIndex);
    Object value;/*from w  w  w . ja  v  a  2  s  . c  o  m*/
    switch (type) {
    case Cursor.FIELD_TYPE_BLOB:
        value = cursor.getBlob(columnIndex);
        break;
    case Cursor.FIELD_TYPE_FLOAT:
        value = cursor.getFloat(columnIndex);
        break;
    case Cursor.FIELD_TYPE_INTEGER:
        value = cursor.getLong(columnIndex);
        break;
    case Cursor.FIELD_TYPE_NULL:
        value = null;
        break;
    case Cursor.FIELD_TYPE_STRING:
        value = cursor.getString(columnIndex);
        break;
    default:
        value = cursor.getString(columnIndex);
    }
    return value;
}

From source file:com.bjorsond.android.timeline.utilities.Utilities.java

public static String getRealPathFromURI(Uri contentUri, Activity a) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = a.managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();//w w  w.ja  va 2s  .  com
    return cursor.getString(column_index);
}

From source file:Main.java

/**
 * Print the content of the cursor/* w  ww  .j  av a  2 s  . com*/
 *
 * @param cursor, The cursor, which content needs to be printed
 * @param logTag, The log tag
 */
public static void printCursorContent(Cursor cursor, String logTag) {
    if (cursor == null) {
        Log.d(logTag, "Cursor is NULL!");
        return;
    }
    final int columnSpace = 2;
    ArrayList<Integer> columnWidth = new ArrayList<Integer>();
    for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
        String value = cursor.getColumnName(columnIndex);
        int maxWidth = value.length();
        if (cursor.moveToFirst()) {
            do {
                try {
                    value = cursor.getString(columnIndex);
                } catch (Exception e) {
                    value = "BLOB";
                    Log.d(logTag, "Get value from " + cursor.getColumnName(columnIndex) + " failed. Caused by "
                            + e.getLocalizedMessage());
                }
                if (!TextUtils.isEmpty(value) && value.length() > maxWidth) {
                    maxWidth = value.length();
                }
            } while (cursor.moveToNext());
        }
        columnWidth.add(maxWidth + columnSpace);
    }
    ArrayList<ArrayList<String>> tableContent = new ArrayList<ArrayList<String>>();
    for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
        ArrayList<String> columnContent = new ArrayList<String>();
        String value = cursor.getColumnName(columnIndex);
        columnContent.add(appendColumnSpaces(value, columnWidth.get(columnIndex)));
        if (cursor.moveToFirst()) {
            do {
                try {
                    value = cursor.getString(columnIndex);
                } catch (Exception e) {
                    value = "BLOB";
                }
                columnContent.add(appendColumnSpaces(value, columnWidth.get(columnIndex)));
            } while (cursor.moveToNext());
        }
        tableContent.add(columnContent);
    }
    // Including the header
    int maxRowIndex = cursor.getCount() + 1;
    for (int rowIndex = 0; rowIndex < maxRowIndex; rowIndex++) {
        StringBuilder rowValues = new StringBuilder();
        for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) {
            ArrayList<String> columnValues = tableContent.get(columnIndex);
            rowValues.append(columnValues.get(rowIndex));
        }
        Log.d(logTag, rowValues.toString());
    }
    // set the cursor back the first item
    cursor.moveToFirst();
}

From source file:com.hivewallet.androidclient.wallet.AddressBookProvider.java

public static AddressBookEntry lookupEntry(final Context context, @Nonnull final String address) {
    String label = null;/*from www.  j a  v  a2  s  .c  o m*/
    String photo = null;

    final Uri uri = contentUri(context.getPackageName()).buildUpon().appendPath(address).build();
    final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            label = cursor.getString(cursor.getColumnIndexOrThrow(KEY_LABEL));
            photo = cursor.getString(cursor.getColumnIndexOrThrow(KEY_PHOTO));
        }

        cursor.close();
    }

    if (label != null) {
        return new AddressBookEntry(address, label, photo);
    } else {
        return null;
    }
}

From source file:de.wikilab.android.friendica01.Max.java

public static String getRealPathFromURI(Context ctx, Uri contentUri) {
    Log.i("FileTypes", "URI = " + contentUri + ", scheme = " + contentUri.getScheme());
    if (contentUri.getScheme().equals("file")) {
        return contentUri.getPath();
    } else {/*from  w  w w . ja  v  a 2 s .  c o m*/
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String res = cursor.getString(column_index);
        cursor.close();
        return res;
    }
}

From source file:za.co.neilson.alarm.database.Database.java

public static Alarm getAlarm(int id) {
    // TODO Auto-generated method stub
    String[] columns = new String[] { COLUMN_ALARM_ID, COLUMN_ALARM_ACTIVE, COLUMN_ALARM_TIME,
            COLUMN_ALARM_DAYS, COLUMN_ALARM_DIFFICULTY, COLUMN_ALARM_TONE, COLUMN_ALARM_VIBRATE,
            COLUMN_ALARM_NAME };/*  w  ww  .j a  v a 2s . c  o  m*/
    Cursor c = getDatabase().query(ALARM_TABLE, columns, COLUMN_ALARM_ID + "=" + id, null, null, null, null);
    Alarm alarm = null;

    if (c.moveToFirst()) {

        alarm = new Alarm();
        alarm.setId(c.getInt(1));
        alarm.setAlarmActive(c.getInt(2) == 1);
        alarm.setAlarmTime(c.getString(3));
        byte[] repeatDaysBytes = c.getBlob(4);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(repeatDaysBytes);
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Alarm.Day[] repeatDays;
            Object object = objectInputStream.readObject();
            if (object instanceof Alarm.Day[]) {
                repeatDays = (Alarm.Day[]) object;
                alarm.setDays(repeatDays);
            }
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        alarm.setDifficulty(Difficulty.values()[c.getInt(5)]);
        alarm.setAlarmTonePath(c.getString(6));
        alarm.setVibrate(c.getInt(7) == 1);
        alarm.setAlarmName(c.getString(8));
    }
    c.close();
    return alarm;
}

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

/**
 * Returns the uri for the media. It will return, in order, the local uri if it's present, the
 * low-res uri (if {@link #shouldShowLowQuality(Context)} says so), or the full-res URI.
 *
 * @param context/* www  .  j  a v  a 2 s  .  c om*/
 * @param c
 * @return null or a url pointing to the media
 */
public static Uri getMedia(Context context, Cursor c) {
    Uri media;

    final int mediaLocalCol = c.getColumnIndexOrThrow(_LOCAL_URI);
    final int mediaCol = c.getColumnIndexOrThrow(_MEDIA_URL);

    if (!c.isNull(mediaLocalCol)) {
        media = Uri.parse(c.getString(mediaLocalCol));

    } else if (shouldShowLowQuality(context)) {
        final int mediaLowResCol = c.getColumnIndexOrThrow(_LOW_BITRATE_URL);
        media = Uri.parse(c.getString(mediaLowResCol));

    } else if (!c.isNull(mediaCol)) {
        media = Uri.parse(c.getString(mediaCol));

    } else {
        media = null;
    }

    return media;
}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static String getNameFromEmail(Context context, String email) {

    if (context == null) {
        return null;
    }/*from ww  w .ja  va 2s  .c  o m*/

    if (loadContacts(context)) {
        try {
            String name = MainApplication.contacts.getJSONObject(email).getString("name");
            Log.e(TAG, "getNameFromEmail - Email: " + email + ", Name: " + name);
            return name;
        } catch (JSONException e) {
            Log.e(TAG, "getNameFromEmail - failed: " + email);
        }
    }

    Log.e(TAG, "getNameFromEmail - Name queried: " + email);
    Cursor emailCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            null, ContactsContract.CommonDataKinds.Email.DATA + "='" + email + "'", null, null);
    if (emailCursor == null) {
        return "???";
    }
    if (emailCursor.moveToNext()) {
        String name = emailCursor.getString(
                emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY));
        Log.e(TAG, "getNameFromEmail - Email: " + email + ", Name: " + name);
        emailCursor.close();
        return name;
    }
    emailCursor.close();
    return email;
}