Example usage for android.database Cursor getColumnIndexOrThrow

List of usage examples for android.database Cursor getColumnIndexOrThrow

Introduction

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

Prototype

int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;

Source Link

Document

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

Usage

From source file:foam.zizim.android.BoskoiService.java

public static BlogData getBlogData(int id) {
    Cursor cursor;
    cursor = BoskoiApplication.mDb.fetchBlogById(id);
    BlogData blogData = null;//from   w ww . j  av a  2  s  .  c o  m

    if (cursor.moveToFirst()) {
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_ID);
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_TITLE);
        int dateIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DATE);
        int descIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DESCRIPTION);

        int linkIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_LINK);

        do {

            blogData = new BlogData();

            blogData.setId(Util.toInt(cursor.getString(idIndex)));
            blogData.setTitle(cursor.getString(titleIndex));
            blogData.setDescription(cursor.getString(descIndex));
            blogData.setLink(cursor.getString(linkIndex));
            blogData.setDate(cursor.getString(dateIndex));

        } while (cursor.moveToNext());

    }
    cursor.close();

    return blogData;
}

From source file:com.popcorntime.apps.remote.utils.Utils.java

public static String getRealPathFromUri(Context context, Uri contentUri) {
    Log.i("uri", contentUri.toString());
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        Cursor cursor = null;
        try {//from  www . j a v a 2 s. com

            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else {
        Uri uri = contentUri;
        // DocumentProvider
        if (DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }

                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri2 = ContentUris
                        .withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri2, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri2 = null;
                if ("image".equals(type)) {
                    contentUri2 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    //contentUri2 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    //contentUri2 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[] { split[1] };

                return getDataColumn(context, contentUri2, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }
}

From source file:it.feio.android.omninotes.utils.StorageManager.java

public static String getRealPathFromURI(Context mContext, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = null;
    try {/*from w w  w.  j av  a2  s  .c om*/
        mContext.getContentResolver().query(contentUri, proj, null, null, null);
    } catch (Exception e) {
    }
    if (cursor == null) {
        return null;
    }
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

From source file:foam.zizim.android.BoskoiService.java

public static List<BlogData> getBlogData() {
    Cursor cursor;
    cursor = BoskoiApplication.mDb.fetchAllBlog();
    List blog = new ArrayList<BlogData>();
    if (cursor.moveToFirst()) {
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_ID);
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_TITLE);
        int dateIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DATE);
        int descIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DESCRIPTION);

        int linkIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_LINK);

        do {//from w  w  w.ja v  a2 s. c  o m

            BlogData blogData = new BlogData();

            blogData.setId(Util.toInt(cursor.getString(idIndex)));
            blogData.setTitle(cursor.getString(titleIndex));
            blogData.setDescription(cursor.getString(descIndex));
            blogData.setLink(cursor.getString(linkIndex));
            blogData.setDate(cursor.getString(dateIndex));
            blog.add(blogData);

        } while (cursor.moveToNext());

    }
    cursor.close();

    return blog;
}

From source file:foam.zizim.android.BoskoiService.java

public static List<BlogData> getSimpleBlogData() {
    Cursor cursor;
    cursor = BoskoiApplication.mDb.fetchAllSimpleBlog();
    List blog = new ArrayList<BlogData>();
    if (cursor.moveToFirst()) {
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_ID);
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_TITLE);
        int dateIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_DATE);
        int linkIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.BLOG_LINK);

        do {/* ww  w  .  j  a va  2  s.  co m*/

            BlogData blogData = new BlogData();

            blogData.setId(Util.toInt(cursor.getString(idIndex)));
            blogData.setTitle(cursor.getString(titleIndex));
            blogData.setLink(cursor.getString(linkIndex));
            blogData.setDate(cursor.getString(dateIndex));
            blog.add(blogData);

        } while (cursor.moveToNext());

    }
    cursor.close();

    return blog;
}

From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_communicator.util.FileUtils.java

public static String getRealPathFromURI(Uri contentUri, Context ctx) {
    String[] proj = { MediaStore.Video.Media.DATA };
    Cursor cursor = ((Activity) ctx).managedQuery(contentUri, proj, null, null, null);
    if (cursor != null) {
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();/*w  w w  .  ja  v  a 2 s.  co m*/
        String ret = cursor.getString(columnIndex);

        return ret;
    } else {
        return contentUri.getPath();
    }
}

From source file:foam.zizim.android.BoskoiService.java

public static CategoriesData[] getParentCategories() {
    Cursor cursor = BoskoiApplication.mDb.fetchParentCategories();
    CategoriesData result[] = new CategoriesData[cursor.getCount()];

    int i = 0;//  w ww .j  a  va 2s. co  m
    if (cursor.moveToFirst()) {
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_TITLE);
        int titleNL = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_TITLE_NL);
        int titleLA = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_TITLE_LA);
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_ID);
        int parentId = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_PARENT_ID);
        int color = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_COLOR);
        int desc = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_DESC);

        do {
            CategoriesData cat = new CategoriesData();
            cat.setCategoryId(cursor.getInt(idIndex));
            cat.setCategoryTitle(cursor.getString(titleIndex));
            cat.setCategoryTitleNL(cursor.getString(titleNL));
            cat.setCategoryTitleLA(cursor.getString(titleLA));
            cat.setCategoryParentId(cursor.getInt(parentId));
            cat.setCategoryColor(cursor.getString(color));
            cat.setCategoryDescription(cursor.getString(desc));

            result[i] = cat;

            i++;
        } while (cursor.moveToNext());
    }

    cursor.close();
    return result;
}

From source file:com.android.mms.rcs.FavoriteDetailAdapter.java

private static String getTextMessageDetails(Context context, Cursor cursor, boolean isAppendContentType) {

    StringBuilder details = new StringBuilder();
    Resources res = context.getResources();

    // Message Type: Text message.
    details.append(res.getString(R.string.message_type_label));
    int rcsChatType = cursor
            .getInt(cursor.getColumnIndexOrThrow(FavoriteMessageProvider.FavoriteMessage.CHAT_TYPE));
    boolean isRcsMessage = RcsUtils.isRcsMessage(rcsChatType);
    if (isRcsMessage)
        details.append(res.getString(R.string.rcs_text_message));
    else// w  w  w  .  jav  a 2s.c  om
        details.append(res.getString(R.string.text_message));

    if (isAppendContentType) {
        details.append('\n');
        details.append(res.getString(R.string.message_content_type));
        int msgType = cursor.getInt(cursor.getColumnIndex(FavoriteMessageProvider.FavoriteMessage.MSG_TYPE));
        if (msgType == RcsUtils.RCS_MSG_TYPE_IMAGE)
            details.append(res.getString(R.string.message_content_image));
        else if (msgType == RcsUtils.RCS_MSG_TYPE_AUDIO)
            details.append(res.getString(R.string.message_content_audio));
        else if (msgType == RcsUtils.RCS_MSG_TYPE_VIDEO)
            details.append(res.getString(R.string.message_content_video));
        else if (msgType == RcsUtils.RCS_MSG_TYPE_MAP)
            details.append(res.getString(R.string.message_content_map));
        else if (msgType == RcsUtils.RCS_MSG_TYPE_VCARD)
            details.append(res.getString(R.string.message_content_vcard));
        else if (msgType == RcsUtils.RCS_MSG_TYPE_CAIYUNFILE)
            details.append(res.getString(R.string.msg_type_CaiYun));
        else
            details.append(res.getString(R.string.message_content_text));
    }
    // Address: ***
    details.append('\n');
    int smsType = cursor
            .getInt(cursor.getColumnIndexOrThrow(FavoriteMessageProvider.FavoriteMessage.DIRECTION));
    int chatType = cursor
            .getInt(cursor.getColumnIndexOrThrow(FavoriteMessageProvider.FavoriteMessage.CHAT_TYPE));
    if ((smsType != Constants.MessageConstants.CONST_DIRECTION_RECEIVE)
            && (chatType != Constants.MessageConstants.CONST_CHAT_GROUP)) {
        details.append(res.getString(R.string.to_address_label));
    } else {
        details.append(res.getString(R.string.from_label));
    }

    String address = "";
    address = cursor.getString(cursor.getColumnIndexOrThrow(FavoriteMessageProvider.FavoriteMessage.NUMBER));
    details.append(address);
    // date
    details.append('\n');
    long date = cursor.getLong(cursor.getColumnIndexOrThrow(FavoriteMessageProvider.FavoriteMessage.DATE));
    details.append(MessageUtils.formatTimeStampString(context, date, true));

    return details.toString();
}

From source file:foam.zizim.android.BoskoiService.java

public static CategoriesData[] getCategoriesFromParent(int categoryId) {
    Cursor cursor = BoskoiApplication.mDb.fetchCategoriesFromParent(categoryId);
    CategoriesData result[] = new CategoriesData[cursor.getCount()];

    int i = 0;/*from   w  w w .j  a va2  s.  com*/
    if (cursor.moveToFirst()) {
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_TITLE);
        int titleNL = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_TITLE_NL);
        int titleLA = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_TITLE_LA);
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_ID);
        int parentId = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_PARENT_ID);
        int color = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_COLOR);
        int desc = cursor.getColumnIndexOrThrow(BoskoiDatabase.CATEGORY_DESC);

        do {
            CategoriesData cat = new CategoriesData();
            cat.setCategoryId(cursor.getInt(idIndex));
            cat.setCategoryTitle(cursor.getString(titleIndex));
            cat.setCategoryTitleNL(cursor.getString(titleNL));
            cat.setCategoryTitleLA(cursor.getString(titleLA));
            cat.setCategoryParentId(cursor.getInt(parentId));
            cat.setCategoryColor(cursor.getString(color));
            cat.setCategoryDescription(cursor.getString(desc));

            result[i] = cat;

            i++;
        } while (cursor.moveToNext());
    }

    cursor.close();
    return result;
}

From source file:edu.stanford.mobisocial.dungbeetle.model.DbObject.java

/**
 * @param v the view to bind/*from   w  ww  .  java 2  s  . c o m*/
 * @param context standard activity context
 * @param c the cursor source for the object in the db object table.
 * Must include _id in the projection.
 * 
 * @param allowInteractions controls whether the bound view is
 * allowed to intercept touch events and do its own processing.
 */
public static void bindView(View v, final Context context, Cursor cursor, boolean allowInteractions) {
    TextView nameText = (TextView) v.findViewById(R.id.name_text);
    ViewGroup frame = (ViewGroup) v.findViewById(R.id.object_content);
    frame.removeAllViews();

    // make sure we have all the columns we need
    Long objId = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_ID));
    String[] projection = null;
    String selection = DbObj.COL_ID + " = ?";
    String[] selectionArgs = new String[] { Long.toString(objId) };
    String sortOrder = null;
    Cursor c = context.getContentResolver().query(DbObj.OBJ_URI, projection, selection, selectionArgs,
            sortOrder);
    if (!c.moveToFirst()) {
        Log.w(TAG, "could not find obj " + objId);
        c.close();
        return;
    }
    DbObj obj = App.instance().getMusubi().objForCursor(c);
    if (obj == null) {
        nameText.setText("Failed to access database.");
        Log.e("DbObject", "cursor was null for bindView of DbObject");
        return;
    }
    DbUser sender = obj.getSender();
    Long timestamp = c.getLong(c.getColumnIndexOrThrow(DbObj.COL_TIMESTAMP));
    Long hash = obj.getHash();
    short deleted = c.getShort(c.getColumnIndexOrThrow(DELETED));
    String feedName = obj.getFeedName();
    String type = obj.getType();
    Date date = new Date(timestamp);
    c.close();
    c = null;

    if (sender == null) {
        nameText.setText("Message from unknown contact.");
        return;
    }
    nameText.setText(sender.getName());

    final ImageView icon = (ImageView) v.findViewById(R.id.icon);
    if (sViewProfileAction == null) {
        sViewProfileAction = new OnClickViewProfile((Activity) context);
    }
    icon.setTag(sender.getLocalId());
    if (allowInteractions) {
        icon.setOnClickListener(sViewProfileAction);
        v.setTag(objId);
    }
    icon.setImageBitmap(sender.getPicture());

    if (deleted == 1) {
        v.setBackgroundColor(sDeletedColor);
    } else {
        v.setBackgroundColor(Color.TRANSPARENT);
    }

    TextView timeText = (TextView) v.findViewById(R.id.time_text);
    timeText.setText(RelativeDate.getRelativeDate(date));

    frame.setTag(objId); // TODO: error prone! This is database id
    frame.setTag(R.id.object_entry, cursor.getPosition()); // this is cursor id
    FeedRenderer renderer = DbObjects.getFeedRenderer(type);
    if (renderer != null) {
        renderer.render(context, frame, obj, allowInteractions);
    }

    if (!allowInteractions) {
        v.findViewById(R.id.obj_attachments_icon).setVisibility(View.GONE);
        v.findViewById(R.id.obj_attachments).setVisibility(View.GONE);
    } else {
        if (!MusubiBaseActivity.isDeveloperModeEnabled(context)) {
            v.findViewById(R.id.obj_attachments_icon).setVisibility(View.GONE);
            v.findViewById(R.id.obj_attachments).setVisibility(View.GONE);
        } else {
            ImageView attachmentCountButton = (ImageView) v.findViewById(R.id.obj_attachments_icon);
            TextView attachmentCountText = (TextView) v.findViewById(R.id.obj_attachments);
            attachmentCountButton.setVisibility(View.VISIBLE);

            if (hash == 0) {
                attachmentCountButton.setVisibility(View.GONE);
            } else {
                //int color = DbObject.colorFor(hash);
                boolean selfPost = false;
                DBHelper helper = new DBHelper(context);
                try {
                    Cursor attachments = obj.getSubfeed().query("type=?", new String[] { LikeObj.TYPE });
                    try {
                        attachmentCountText.setText("+" + attachments.getCount());

                        if (attachments.moveToFirst()) {
                            while (!attachments.isAfterLast()) {
                                if (attachments.getInt(attachments.getColumnIndex(CONTACT_ID)) == -666) {
                                    selfPost = true;
                                    break;
                                }
                                attachments.moveToNext();

                            }
                        }
                    } finally {
                        attachments.close();
                    }
                } finally {
                    helper.close();
                }
                if (selfPost) {
                    attachmentCountButton.setImageResource(R.drawable.ic_menu_love_red);
                } else {
                    attachmentCountButton.setImageResource(R.drawable.ic_menu_love);
                }
                attachmentCountText.setTag(R.id.object_entry, hash);
                attachmentCountText.setTag(R.id.feed_label, Feed.uriForName(feedName));
                attachmentCountText.setOnClickListener(LikeListener.getInstance(context));
            }
        }
    }
}