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:com.battlelancer.seriesguide.adapters.EpisodesAdapter.java

/**
 * Get the item position in the data set, or the position of the first item if it is not found.
 *///from ww w . ja  va 2s .c  o  m
public int getItemPosition(long itemId) {
    Cursor cursor = getCursor();
    if (cursor != null) {
        int rowId = cursor.getColumnIndexOrThrow("_id");
        for (int position = 0; position < cursor.getCount(); position++) {
            if (!cursor.moveToPosition(position)) {
                return 0;
            }
            if (cursor.getLong(rowId) == itemId) {
                return position;
            }
        }
    }

    return 0;
}

From source file:com.chatwing.whitelabel.fragments.ConversationsFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mDelegate.inject(this);
    LogUtils.v("Load conversation boxes");
    mEmptyTextView = (TextView) view.findViewById(android.R.id.empty);
    mConversationsListView = (ListView) view.findViewById(R.id.listview);
    mListAdapter = new ConversationsAdapter(getActivity(), mUserManager.getCurrentUser(), null, 0);

    mConversationsListView.setAdapter(mListAdapter);
    mConversationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override/*from   w w w.  ja  v a 2s  .  c o m*/
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Cursor cursor = (Cursor) mListAdapter.getItem(position);
            int columnConversationIndex = cursor.getColumnIndexOrThrow(ConversationTable.CONVERSATION_ID);
            String conversationId = cursor.getString(columnConversationIndex);
            mBus.post(new UserSelectedConversationEvent(conversationId));
        }
    });

    view.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDelegate.back(ConversationsFragment.this);
        }
    });

    loadConversationsFromDb();
}

From source file:com.google.android.demos.jamendo.widget.SimpleFeedAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    for (int i = 0; i < mFrom.length; i++) {
        final View v = view.findViewById(mTo[i]);
        if (v != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(mFrom[i]);
            if (!setViewValue(v, cursor, columnIndex)) {
                String value = cursor.getString(columnIndex);
                if (value == null) {
                    value = "";
                }// www . j a  va  2 s.c om
                if (v instanceof TextView) {
                    TextView textView = (TextView) v;
                    textView.setText(value);
                } else if (v instanceof ImageView) {
                    ImageView imageView = (ImageView) v;
                    try {
                        imageView.setImageResource(Integer.parseInt(value));
                    } catch (NumberFormatException e) {
                        if (TextUtils.isEmpty(value)) {
                            value = mDefaultImageUrl;
                        }
                        mImageLoader.bind(this, imageView, value);
                    }
                } else {
                    throw new IllegalStateException(v.getClass().getName() + " is not a "
                            + " view that can be bound by this " + getClass().getSimpleName());
                }
            }
        }
    }
}

From source file:com.google.android.demos.jamendo.app.AlbumActivity.java

private void share() {
    long id = ContentUris.parseId(getIntent().getData());
    Uri uri = ContentUris.withAppendedId(BASE_URI, id);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    Cursor cursor = mHeaderAdapter.getCursor();
    if (cursor != null && cursor.moveToFirst()) {
        String albumName = cursor.getString(cursor.getColumnIndexOrThrow(Albums.NAME));
        String artistName = cursor.getString(cursor.getColumnIndexOrThrow(Artists.NAME));
        String template = getString(R.string.jamendo_template_album_artist);
        String subject = MessageFormat.format(template, albumName, artistName);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    }/*from  w  w w .ja  v a  2 s . co  m*/
    intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(uri));
    intent = Intent.createChooser(intent, null);
    startActivity(intent);
}

From source file:at.wada811.imageslider.MainActivity.java

private Bitmap loadBitmap(Cursor cursor) {
    LogUtils.d("Position: " + cursor.getPosition());
    String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
    //        int orientation = getOrientationFromExif(filePath);
    //        LogUtils.d("orientation: " + orientation);
    LogUtils.d(filePath);//from   w  ww .j a v  a  2s  .  c om
    Bitmap bitmap = BitmapUtils.createBitmapFromFile(filePath);
    //        bitmap = BitmapUtils.rotate(bitmap, orientation);
    return bitmap;
}

From source file:com.samuelcastro.cordova.InstagramSharePlugin.java

public String getRealVideoPathFromURI(Uri contentUri) {
    try {//from  ww  w .  ja  v  a 2s  . co  m
        String[] proj = { MediaStore.Video.Media.DATA };
        Cursor cursor = cordova.getActivity().getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception e) {
        return contentUri.getPath();
    }
}

From source file:com.android.providers.contacts.ContactsSyncAdapter.java

private static void cursorToContactsElement(ContactsElement element, Cursor c, HashMap<Integer, Byte> map) {
    final int typeIndex = c.getColumnIndexOrThrow("type");
    final int labelIndex = c.getColumnIndexOrThrow("label");
    final int isPrimaryIndex = c.getColumnIndexOrThrow("isprimary");

    element.setLabel(c.getString(labelIndex));
    element.setType(map.get(c.getInt(typeIndex)));
    element.setIsPrimary(c.getInt(isPrimaryIndex) != 0);
}

From source file:com.gaba.alex.trafficincidents.Adapter.IncidentsAdapter.java

@Override
public void bindView(View view, final Context context, Cursor cursor) {
    super.bindView(view, context, cursor);

    final int type = cursor.getInt(cursor.getColumnIndexOrThrow(IncidentsColumns.TYPE));
    final int severity = cursor.getInt(cursor.getColumnIndexOrThrow(IncidentsColumns.SEVERITY));
    final double lat = cursor.getDouble(cursor.getColumnIndexOrThrow(IncidentsColumns.LAT));
    final double lng = cursor.getDouble(cursor.getColumnIndexOrThrow(IncidentsColumns.LNG));
    final double toLat = cursor.getDouble(cursor.getColumnIndexOrThrow(IncidentsColumns.TO_LAT));
    final double toLng = cursor.getDouble(cursor.getColumnIndexOrThrow(IncidentsColumns.TO_LNG));
    final long dateInMillis = Long
            .parseLong(cursor.getString(cursor.getColumnIndexOrThrow(IncidentsColumns.END_DATE)));
    final boolean roadClosed = Boolean
            .valueOf(cursor.getString(cursor.getColumnIndexOrThrow(IncidentsColumns.ROAD_CLOSED)));
    final String description = cursor.getString(cursor.getColumnIndexOrThrow(IncidentsColumns.DESCRIPTION));

    TextView typeTextView = (TextView) view.findViewById(R.id.incident_type);
    if (typeTextView != null) {
        typeTextView.setText(Utility.getIncidentType(mContext, type));
    }//  w  w  w  .j a  v  a 2 s  .  co m

    TextView severityTextView = (TextView) view.findViewById(R.id.incident_severity);
    if (severityTextView != null) {
        severityTextView.setText(Utility.getIncidentSeverity(mContext, severity));
        severityTextView.setTextColor(ContextCompat.getColor(mContext, Utility.getIncidentColor(severity)));
    }

    TextView roadClosedTextView = (TextView) view.findViewById(R.id.incident_road_closed);
    if (roadClosedTextView != null) {
        roadClosedTextView.setText(String.format("%s: ", mContext.getString(R.string.road_closed)));
    }

    TextView roadClosedContentTextView = (TextView) view.findViewById(R.id.incident_road_closed_content);
    if (roadClosedContentTextView != null) {
        roadClosedContentTextView
                .setText(roadClosed ? mContext.getString(R.string.yes) : mContext.getString(R.string.no));
        roadClosedContentTextView.setTextColor(ContextCompat.getColor(mContext,
                roadClosed ? android.R.color.holo_red_light : android.R.color.holo_green_dark));
    }

    TextView dateTextView = (TextView) view.findViewById(R.id.incident_end_date);
    if (dateTextView != null) {
        dateTextView.setText(String.format("%s: ", mContext.getString(R.string.estimated_end_date)));
    }

    TextView dateContentTextView = (TextView) view.findViewById(R.id.incident_end_date_content);
    if (dateContentTextView != null) {
        dateContentTextView.setText(DateFormat.getDateInstance().format(new Date(dateInMillis)));
    }

    TextView descriptionTextView = (TextView) view.findViewById(R.id.incident_description);
    if (descriptionTextView != null) {
        descriptionTextView.setText(String.format("%s: ", mContext.getString(R.string.local_description)));
    }

    TextView descriptionContentTextView = (TextView) view.findViewById(R.id.incident_description_content);
    if (descriptionContentTextView != null) {
        descriptionContentTextView.setText(description);
    }

    ImageView showOnMapImageView = (ImageView) view.findViewById(R.id.show_on_map_button);
    showOnMapImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = Utility.buildShowOnMapIntent(mContext, lat, lng, toLat, toLng, severity,
                    description);
            if (intent.resolveActivity(mContext.getPackageManager()) != null) {
                mContext.startActivity(intent);
            }
        }
    });

    ImageView shareImageView = (ImageView) view.findViewById(R.id.share_button);
    shareImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            String shareText = Utility.getIncidentType(mContext, type) + "\n\n"
                    + mContext.getString(R.string.local_description) + ": " + description + "\n\n"
                    + mContext.getString(R.string.severity) + ": "
                    + Utility.getIncidentSeverity(mContext, severity) + "\n\n"
                    + mContext.getString(R.string.road_closed) + ": "
                    + (roadClosed ? mContext.getString(R.string.yes) : mContext.getString(R.string.no)) + "\n\n"
                    + mContext.getString(R.string.provided_by) + ": " + mContext.getString(R.string.app_name);
            intent.putExtra(Intent.EXTRA_TEXT, shareText);
            if (intent.resolveActivity(mContext.getPackageManager()) != null) {
                mContext.startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            }
        }
    });
}

From source file:ca.ualberta.cmput301.t03.inventory.AddItemView.java

/**
 * Used to get the path of the file selected in the gallery selector. This is called in the
 * onActivityResult handler./*www  .j ava 2 s . c o m*/
 *
 * Code used:
 * http://stackoverflow.com/questions/27874038/how-to-make-intent-chooser-for-camera-or-gallery-application-in-android-like-wha
 * @param uri the uri returned by the gallery intent
 * @param activity this activity
 * @return
 */
public String getPath(Uri uri, Activity activity) {
    String[] projection = { MediaStore.MediaColumns.DATA };
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

From source file:com.google.firebase.udacity.greenthumb.ShoppingCartActivity.java

/**
 * Returns the total price of the shopping cart
 * @param cursor The cursor containing the items in the cart
 * @return The total price of the cart/*from w ww. j a  va 2s.co m*/
 */
private int calculateTotal(Cursor cursor) {
    if (cursor == null) {
        return 0;
    }
    int total = 0;
    while (cursor.moveToNext()) {
        int quantity = cursor.getInt(cursor.getColumnIndexOrThrow(PlantEntry.COLUMN_CART_QUANTITY));
        int price = cursor.getInt(cursor.getColumnIndexOrThrow(PlantEntry.COLUMN_PRICE));
        total = total + (quantity * price);
    }
    return total;
}