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:org.jared.synodroid.ds.utils.Utils.java

public static String getContentName(ContentResolver resolver, Uri uri) {
    Cursor cursor = resolver.query(uri, null, null, null, null);
    cursor.moveToFirst();/*from   w w w. java2  s . c o m*/
    int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
    if (nameIndex >= 0) {
        return cursor.getString(nameIndex);
    } else {
        return null;
    }
}

From source file:Main.java

@Nullable
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())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;
        try {//from w w  w  .j ava2 s  . 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 images on API 13+
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (IllegalArgumentException e) {
            // Google Drive images
            return getFromMediaUriPfd(context, resolver, uri);
        } catch (SecurityException ignored) {
            // Nothing we can do
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}

From source file:Main.java

public static boolean haveCalendars(Activity ctx) {

    boolean exists = false;

    String[] projection = new String[] { "_id", "name" };
    Uri calendars = Uri.parse("content://calendar/calendars");

    // Calendar uri changes for 2.2
    Uri calendars_2_2 = Uri.parse("content://com.android.calendar/calendars");
    Cursor managedCursor = ctx.managedQuery(calendars, projection, null, null, null);

    if (managedCursor == null || managedCursor.getCount() == 0)
        managedCursor = ctx.managedQuery(calendars_2_2, projection, null, null, null);

    if (managedCursor != null && managedCursor.moveToFirst()) {
        String calName;/*  w  w w  .  ja  v a  2  s . com*/
        String calId;
        int nameColumn = managedCursor.getColumnIndex("name");
        int idColumn = managedCursor.getColumnIndex("_id");
        do {
            calName = managedCursor.getString(nameColumn);
            calId = managedCursor.getString(idColumn);
            exists = true;
        } while (managedCursor.moveToNext());

        managedCursor.close();
    }

    return exists;
}

From source file:Main.java

public static String queryAudioName(Context context, Uri name) {
    String audioId;/*  w  w w  . j a va  2 s  .c  om*/
    String uriName = name.toString();
    uriName = uriName.substring(uriName.lastIndexOf("/") + 1);
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
            null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        int counter = cursor.getCount();
        for (int j = 0; j < counter; j++) {
            audioId = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));

            if (uriName.equals(audioId)) {
                uriName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            } else {
                Ringtone ringtone = RingtoneManager.getRingtone(context, name);
                uriName = ringtone.getTitle(context);
                break;
            }
            cursor.moveToNext();

        }
        cursor.close();
    } else {
        try {
            Ringtone ringtone = RingtoneManager.getRingtone(context, name);
            uriName = ringtone.getTitle(context);
        } catch (Exception e) {
            return uriName;
        }
    }
    return uriName;
}

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

/**
 * @param c a cursor pointing at an item's row
 * @return true if the item is editable by the logged-in user.
 *///from  w ww  .j  ava  2 s  .  co  m
public static boolean canEdit(Context context, Cursor c) {
    final String privacy = c.getString(c.getColumnIndex(_PRIVACY));
    final String useruri = Authenticator.getUserUri(context);
    return privacy == null || useruri == null || useruri.length() == 0
            || useruri.equals(c.getString(c.getColumnIndex(_AUTHOR_URI)));
}

From source file:curso.android.DAO.RespostaDAO.java

public static Resposta getPerguntaById(Resposta p) {
    Cursor cursor = null;
    try {//from   w  w w  .  jav a  2 s. c o  m

        cursor = Const.db.rawQuery("SELECT * FROM resposta WHERE asw_id = " + p.getAsw_id(), null);

        if (cursor.getCount() > 0) {
            int idIndex = cursor.getColumnIndex("asw_id");
            int askIndex = cursor.getColumnIndex("ask_id");
            int userIndex = cursor.getColumnIndex("user_id");
            int respostaIndex = cursor.getColumnIndex("asw_resposta");
            int nameIndex = cursor.getColumnIndex("user_name");
            cursor.moveToFirst();
            do {
                Long id = Long.valueOf(cursor.getInt(idIndex));
                Long ask = Long.valueOf(cursor.getString(askIndex));
                Long user = Long.valueOf(cursor.getString(userIndex));
                String resposta = cursor.getString(respostaIndex);
                String user_name = cursor.getString(nameIndex);

                Resposta answer = new Resposta();
                answer.setAsw_id(id);
                answer.setAsk_id(ask);
                answer.setUser_id(user);
                answer.setAsw_resposta(resposta);
                answer.setUser_name(user_name);

                return answer;

            } while (!cursor.isAfterLast());
        }

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

From source file:Main.java

public static int getStatus(Context context) {
    Cursor query = null;
    String packageName = context.getPackageName();
    try {//from   w  ww.  java2s  .  co m
        query = context.getContentResolver().query(Uri.parse("content://com.lbe.security.miui.permmgr/active"),
                null, "pkgName=?", new String[] { packageName }, null);
        if (query == null) {
            return 0;
        }
        if (query.moveToFirst()) {
            int status = query.getInt(query.getColumnIndex("userAccept"));
            if (query == null) {
                return status;
            }
        }
        query.close();
    } catch (Exception e) {

        return -1;
    }

    return -1;
}

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

/**
 * @param c//from  w  w  w  .ja  v a2 s.c o  m
 * @return true if there is a low-res copy of the media
 */
public static boolean hasLowBitrate(Cursor c) {
    final String mediaString = c.getString(c.getColumnIndex(CastMedia._LOW_BITRATE_URL));

    return mediaString != null && mediaString.length() > 0;
}

From source file:net.smart_json_database.JSONEntity.java

public static JSONEntity loadFromCursor(Cursor c) throws JSONException {
    int col_id = c.getColumnIndex("json_uid");
    int col_createDate = c.getColumnIndex("createDate");
    int col_updateDate = c.getColumnIndex("updateDate");
    int col_data = c.getColumnIndex("data");
    int col_type = c.getColumnIndex("type");
    JSONEntity entity = new JSONEntity();
    entity.setUid(c.getInt(col_id));/*from  w  w w  .jav a 2 s .  co  m*/
    entity.setCreationDate(Util.ParseDateFromString(c.getString(col_createDate)));
    entity.setUpdateDate(Util.ParseDateFromString(c.getString(col_updateDate)));
    entity.setData(new JSONObject(c.getString(col_data)));
    entity.setType(c.getString(col_type));
    return entity;
}

From source file:curso.android.DAO.PerguntaDAO.java

public static Pergunta getPerguntaById(Pergunta p) {
    Cursor cursor = null;
    try {//  ww  w  .j a va 2 s.  com

        cursor = Const.db.rawQuery("SELECT * FROM pergunta WHERE ask_id = " + p.getAsk_id(), null);

        if (cursor.getCount() > 0) {
            int idIndex = cursor.getColumnIndex("ask_id");
            int userIndex = cursor.getColumnIndex("user_id");
            int perguntaIndex = cursor.getColumnIndex("ask_pergunta");
            int nomeIndex = cursor.getColumnIndex("user_nome");

            cursor.moveToFirst();
            do {
                Long id = Long.valueOf(cursor.getInt(idIndex));
                Long user = Long.valueOf(cursor.getString(userIndex));
                String pergunta = cursor.getString(perguntaIndex);
                String user_name = cursor.getString(nomeIndex);

                Pergunta ask = new Pergunta();
                ask.setAsk_id(id);
                ask.setUser_id(user);
                ask.setAsk_pergunta(pergunta);
                ask.setUser_name(user_name);

                return ask;

            } while (!cursor.isAfterLast());
        }

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