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:fr.mixit.android.io.JsonHandlerApplyTalks.java

private static boolean isItemUpdated(Uri uri, JSONObject item, ContentResolver resolver) throws JSONException {
    final Cursor cursor = resolver.query(uri, MixItContract.Sessions.PROJ_DETAIL.PROJECTION, null, null, null);
    try {// w w w.ja  v  a  2  s .  co m
        if (!cursor.moveToFirst()) {
            return false;
        }

        String curTitle = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.TITLE);
        if (TextUtils.isEmpty(curTitle)) {
            curTitle = EMPTY;
        }

        String curSummary = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.SUMMARY);
        if (TextUtils.isEmpty(curSummary)) {
            curSummary = EMPTY;
        }

        String curDesc = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.DESC);
        if (TextUtils.isEmpty(curDesc)) {
            curDesc = EMPTY;
        }

        final long curStart = cursor.getLong(MixItContract.Sessions.PROJ_DETAIL.START);
        final long curEnd = cursor.getLong(MixItContract.Sessions.PROJ_DETAIL.END);

        String curRoomId = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.ROOM_ID);
        if (TextUtils.isEmpty(curRoomId)) {
            curRoomId = EMPTY;
        }

        final int curNbVotes = cursor.getInt(MixItContract.Sessions.PROJ_DETAIL.NB_VOTES);
        // final int curMyVote = cursor.getInt(MixItContract.Sessions.PROJ_DETAIL.MY_VOTE);
        // final int curIsFavorite = cursor.getInt(MixItContract.Sessions.PROJ_DETAIL.IS_FAVORITE);

        String curFormat = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.FORMAT);
        if (TextUtils.isEmpty(curFormat)) {
            curFormat = EMPTY;
        }

        String curLevel = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.LEVEL);
        if (TextUtils.isEmpty(curLevel)) {
            curLevel = EMPTY;
        }

        String curLang = cursor.getString(MixItContract.Sessions.PROJ_DETAIL.LANG);
        if (TextUtils.isEmpty(curLang)) {
            curLang = EMPTY;
        }

        final String newTitle = item.has(TAG_TITLE) ? item.getString(TAG_TITLE).trim() : curTitle;
        final String newSummary = item.has(TAG_SUMMARY) ? item.getString(TAG_SUMMARY).trim() : curSummary;
        final String newDesc = item.has(TAG_DESC) ? item.getString(TAG_DESC).trim() : curDesc;
        long newStart = 0;
        if (item.has(TAG_START)) {
            final String start = item.getString(TAG_START);
            newStart = DateUtils.parseISO8601(start);
        } else {
            newStart = curStart;
        }
        long newEnd = 0;
        if (item.has(TAG_END)) {
            final String end = item.getString(TAG_END);
            newEnd = DateUtils.parseISO8601(end);
        } else {
            newEnd = curEnd;
        }
        final String newRoomId = item.has(TAG_ROOM) ? item.getString(TAG_ROOM).trim() : curRoomId;
        final int newNbVotes = item.has(TAG_NB_VOTES) ? item.getInt(TAG_NB_VOTES) : curNbVotes;
        // final int newMyVote = session.has(TAG_MY_VOTE) ? session.getBoolean(TAG_MY_VOTE) ? 1 : 0 : curMyVote;
        // final int newIsFavorite = session.has(TAG_IS_FAVORITE) ? session.getInt(TAG_IS_FAVORITE) : curIsFavorite;
        final String newFormat = item.has(TAG_FORMAT) ? item.getString(TAG_FORMAT).trim() : curFormat;
        final String newLevel = item.has(TAG_LEVEL) ? item.getString(TAG_LEVEL).trim() : curLevel;
        final String newLang = item.has(TAG_LANG) ? item.getString(TAG_LANG).trim() : curLang;

        return !curTitle.equals(newTitle) || //
                !curSummary.equals(newSummary) || //
                !curDesc.equals(newDesc) || //
                curStart != newStart || //
                curEnd != newEnd || //
                !curRoomId.equals(newRoomId) || //
                curNbVotes != newNbVotes || //
                // curMyVote != newMyVote ||
                // curIsFavorite != newIsFavorite || //
                !curFormat.equals(newFormat) || //
                !curLevel.equals(newLevel) || //
                !curLang.equals(newLang);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

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

/**
 * TODO make this pick the set of tags for a set of content.
 *
 * @param cr a content resolver//from  w ww.  j av  a 2s  .c  o m
 * @return the top MAX_POPULAR_TAGS most popular tags in the set, with the most popular first.
 */
public static List<String> getPopularTags(ContentResolver cr) {

    final Map<String, Integer> tagPop = new HashMap<String, Integer>();
    final List<String> popTags;

    final Cursor c = cr.query(Tag.CONTENT_URI, Tag.DEFAULT_PROJECTION, null, null, null);
    final int tagColumn = c.getColumnIndex(Tag._NAME);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        final String tag = c.getString(tagColumn);

        final Integer count = tagPop.get(tag);
        if (count == null) {
            tagPop.put(tag, 1);
        } else {
            tagPop.put(tag, count + 1);
        }
    }
    c.close();

    popTags = new ArrayList<String>(tagPop.keySet());

    Collections.sort(popTags, new Comparator<String>() {
        public int compare(String object1, String object2) {
            return tagPop.get(object2).compareTo(tagPop.get(object1));
        }

    });
    int limit;
    if (popTags.size() < MAX_POPULAR_TAGS) {
        limit = popTags.size();
    } else {
        limit = MAX_POPULAR_TAGS;
    }
    return popTags.subList(0, limit);
}

From source file:Main.java

@TargetApi(19)
public static File getFromMediaUri(Context context, ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    if (SCHEME_FILE.equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if (SCHEME_CONTENT.equals(uri.getScheme())) {
        String filePath = "";
        if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;/*from   w w  w  . jav a  2s. c  om*/
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            }
            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            Cursor cursor = null;
            final String column = "_data";
            final String[] projection = { column };

            try {
                cursor = context.getContentResolver().query(contentUri, projection, selection, selectionArgs,
                        null);
                if (cursor != null && cursor.moveToFirst()) {
                    final int column_index = cursor.getColumnIndexOrThrow(column);
                    filePath = cursor.getString(column_index);
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }
        } else {
            final String[] filePathColumn = { MediaStore.MediaColumns.DATA,
                    MediaStore.MediaColumns.DISPLAY_NAME };
            Cursor cursor = null;
            try {
                cursor = resolver.query(uri, filePathColumn, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    final int columnIndex = (uri.toString()
                            .startsWith("content://com.google.android.gallery3d"))
                                    ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                                    : cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                    // Picasa images on API 13+
                    if (columnIndex != -1) {
                        filePath = cursor.getString(columnIndex);
                    }
                }
            } catch (IllegalArgumentException e) {
                // Google Drive images
                return getFromMediaUriPfd(context, resolver, uri);
            } catch (SecurityException ignored) {
                // Nothing we can do
            } finally {
                if (cursor != null)
                    cursor.close();
            }
        }

        if (!TextUtils.isEmpty(filePath)) {
            return new File(filePath);
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context       The context.//  w w w . j a va2s.  c  o m
 * @param uri           The Uri to query.
 * @param selection     (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 * @author paulburke
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            if (DEBUG)
                DatabaseUtils.dumpCursor(cursor);

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } catch (IllegalArgumentException ex) {
        Log.i(TAG, "getDataColumn: _data", ex);
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:org.zywx.wbpalmstar.engine.EUtil.java

public static java.net.Proxy checkJavaProxy(Context context) {
    java.net.Proxy proxy = null;//w ww  .  j  a  v a 2 s.  c  o m
    if (!wifiEnable(context)) {// ??APN
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            int proxyPort = mCursor.getInt(mCursor.getColumnIndex("port"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                if (0 == proxyPort) {
                    proxyPort = 80;
                }
                proxy = new java.net.Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyStr, proxyPort));
            }
            mCursor.close();
        }
    }
    return proxy;
}

From source file:org.zywx.wbpalmstar.engine.EUtil.java

public static HttpHost checkAndroidProxy(Context context) {
    HttpHost proxy = null;/*  www . j  a va 2  s  . c  o m*/
    if (!wifiEnable(context)) {// ??APN
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            int proxyPort = mCursor.getInt(mCursor.getColumnIndex("port"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                if (0 == proxyPort) {
                    proxyPort = 80;
                }
                proxy = new HttpHost(proxyStr, proxyPort);
            }
            mCursor.close();
        }
    }
    return proxy;
}

From source file:com.adjust.sdk.Util.java

protected static String getAttributionId(final Context context) {
    try {/*  www .  j a v  a 2s. co m*/
        final ContentResolver contentResolver = context.getContentResolver();
        final Uri uri = Uri.parse("content://com.facebook.katana.provider.AttributionIdProvider");
        final String columnName = "aid";
        final String[] projection = { columnName };
        final Cursor cursor = contentResolver.query(uri, projection, null, null, null);

        if (null == cursor) {
            return null;
        }
        if (!cursor.moveToFirst()) {
            cursor.close();
            return null;
        }

        final String attributionId = cursor.getString(cursor.getColumnIndex(columnName));
        cursor.close();
        return attributionId;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static File getFromMediaUri(ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    if (SCHEME_FILE.equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if (SCHEME_CONTENT.equals(uri.getScheme())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;
        try {//w  w  w . ja v  a  2s . c o m
            cursor = resolver.query(uri, filePathColumn, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int columnIndex = (uri.toString().startsWith("content://com.google.android.gallery3d"))
                        ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                        : cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                // Picasa image on newer devices with Honeycomb and up
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (SecurityException ignored) {
            // Nothing we can do
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}

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

public static Bitmap getPhotoFromEmail(Context context, String email) {

    if (context == null || email == null) {
        return null;
    }//from  w w w .  j a  v  a 2  s  .c o m

    Bitmap result = MainApplication.avatarCache.get(email);
    if (result != null) {
        Log.e(TAG, "getPhotoFromEmail IN cache, Email: " + email);
        return result;
    }

    if (loadContacts(context)) {
        try {
            Log.e(TAG, "getPhotoFromEmail - Email: " + email + ", id: "
                    + MainApplication.contacts.getJSONObject(email).getLong("id"));
            result = openPhoto(context, MainApplication.contacts.getJSONObject(email).getLong("id"));
        } catch (JSONException e) {
            Log.e(TAG, "getPhotoFromEmail - failed: " + email);
        }
    } else {
        Log.e(TAG, "getPhotoFromEmail - id queried: " + email);
        Cursor emailCursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Email.DATA + "='" + email + "'", null, null);
        while (emailCursor != null && emailCursor.moveToNext()) {
            Long contactId = Long.valueOf(emailCursor
                    .getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID)));
            result = openPhoto(context, contactId);
        }
        if (emailCursor != null) {
            emailCursor.close();
        }
    }

    if (result == null) {
        String name = getNameFromEmail(context, email);
        result = Utils.getDefaultAvatarInitials(context, name);
    }

    MainApplication.avatarCache.put(email, result);
    return result;
}

From source file:mx.com.adolfogarcia.popularmovies.data.TestUtilities.java

/**
 * Verifies the row the {@link Cursor} is pointing at, contains the same
 * information as that in the {@link ContentValues}.
 *
 * @param expectedValues values expected to be in the row.
 * @param valueCursor data to verify./*from   ww w.  j  ava 2 s . c om*/
 */
static void assertRowEquals(ContentValues expectedValues, Cursor valueCursor) {
    Set<Map.Entry<String, Object>> valueSet = expectedValues.valueSet();
    for (Map.Entry<String, Object> entry : valueSet) {
        String columnName = entry.getKey();
        int columnIdx = valueCursor.getColumnIndex(columnName);
        Assert.assertTrue("Column '" + columnName + "' should exist.", columnIdx != -1);
        String expectedValue = entry.getValue().toString();
        Assert.assertEquals("Value must be equal to'" + expectedValue + "'.", expectedValue,
                valueCursor.getString(columnIdx));
    }
}