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:android.database.DatabaseUtils.java

/**
 * Reads a String out of a field in a Cursor and writes it to a Map.
 *
 * @param cursor The cursor to read from
 * @param field The TEXT field to read//from w w w  .j  a  va2  s  . c  om
 * @param values The {@link ContentValues} to put the value into, with the field as the key
 * @param key The key to store the value with in the map
 */
public static void cursorStringToContentValues(Cursor cursor, String field, ContentValues values, String key) {
    values.put(key, cursor.getString(cursor.getColumnIndexOrThrow(field)));
}

From source file:android.database.DatabaseUtils.java

/**
 * Reads a String out of a field in a Cursor and writes it to an InsertHelper.
 *
 * @param cursor The cursor to read from
 * @param field The TEXT field to read//from   w w w . jav a 2s.  c o m
 * @param inserter The InsertHelper to bind into
 * @param index the index of the bind entry in the InsertHelper
 */
public static void cursorStringToInsertHelper(Cursor cursor, String field, InsertHelper inserter, int index) {
    inserter.bind(index, cursor.getString(cursor.getColumnIndexOrThrow(field)));
}

From source file:au.com.cybersearch2.classyfy.MainActivityTest.java

public void test_search() throws Throwable {
    final MainActivity mainActivity = getActivity();
    Instrumentation instrumentation = getInstrumentation();
    ActivityMonitor am = instrumentation.addMonitor(TitleSearchResultsActivity.class.getName(), null, false);
    assertThat(instrumentation.invokeMenuActionSync(mainActivity, R.id.action_search, 0)).isTrue();
    ActionBar actionBar = mainActivity.getSupportActionBar();
    assertThat(actionBar).isNotNull();//from  ww  w . j av a 2 s . c  o  m
    final FragmentManager sfm = mainActivity.getSupportFragmentManager();
    runTestOnUiThread(new Runnable() {
        public void run() {
            sfm.executePendingTransactions();
        }
    });
    instrumentation.sendCharacterSync(KeyEvent.KEYCODE_I);
    instrumentation.sendCharacterSync(KeyEvent.KEYCODE_N);
    instrumentation.sendCharacterSync(KeyEvent.KEYCODE_F);
    instrumentation.sendCharacterSync(KeyEvent.KEYCODE_ENTER);
    runTestOnUiThread(new Runnable() {
        public void run() {
            sfm.executePendingTransactions();
        }
    });
    TitleSearchResultsActivity titleSearchResultsActivity = (TitleSearchResultsActivity) getInstrumentation()
            .waitForMonitorWithTimeout(am, 10000);
    assertThat(titleSearchResultsActivity).isNotNull();
    assertThat(titleSearchResultsActivity.taskHandle).isNotNull();
    synchronized (titleSearchResultsActivity.taskHandle) {
        titleSearchResultsActivity.taskHandle.wait(10000);
    }
    assertThat(titleSearchResultsActivity.taskHandle.getStatus()).isEqualTo(WorkStatus.FINISHED);
    SimpleCursorAdapter adapter = titleSearchResultsActivity.adapter;
    for (int i = 0; i < adapter.getCount(); i++) {
        Cursor cursor = (Cursor) adapter.getItem(i);
        int column = cursor.getColumnIndexOrThrow(SearchManager.SUGGEST_COLUMN_TEXT_1);
        assertThat(INF_LIST[i]).isEqualTo(cursor.getString(column));
    }
}

From source file:com.orm.androrm.migration.MigrationHelper.java

/**
 * Checks whether a given table name already exists in the database.
 * /*from w  w  w . jav a2 s  . co m*/
 * @param name Name of the table to look up.
 * @return <code>true</code> if one exists <code>false</code> otherwise.
 */
public boolean tableExists(String name) {
    String sql = "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '" + name + "'";

    Cursor c = getCursor(sql);

    while (c.moveToNext()) {
        String table = c.getString(c.getColumnIndexOrThrow("name"));

        if (table.equalsIgnoreCase(name)) {
            close(c);

            return true;
        }
    }

    close(c);
    return false;
}

From source file:com.orm.androrm.migration.MigrationHelper.java

public List<String> getRelationTableNames(String table) {
    List<String> result = new ArrayList<String>();
    String sql = "SELECT name FROM sqlite_master WHERE type='table' AND (name LIKE '" + table
            + "#_%' OR name LIKE '%#_" + table + "' ESCAPE '#')";

    Cursor c = getCursor(sql);

    while (c.moveToNext()) {
        String name = c.getString(c.getColumnIndexOrThrow("name"));

        if (!name.equalsIgnoreCase(table) && (StringUtils.startsWithIgnoreCase(name, table)
                || StringUtils.endsWithIgnoreCase(name, table))) {

            result.add(name);// www.j ava  2  s .c o m
        }
    }

    close(c);
    return result;
}

From source file:com.orm.androrm.migration.MigrationHelper.java

public boolean hasField(Class<? extends Model> model, String name) {
    String table = DatabaseBuilder.getTableName(model);
    String sql = "PRAGMA TABLE_INFO(`" + table + "`)";

    Cursor c = getCursor(sql);

    while (c.moveToNext()) {
        String fieldName = c.getString(c.getColumnIndexOrThrow("name"));

        if (fieldName.equals(name)) {
            close(c);//w w  w. j  a  va 2  s .  com
            return true;
        }
    }

    close(c);
    return false;
}

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

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

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

private void share() {
    Cursor cursor = mHeaderAdapter.getCursor();
    if (cursor != null && cursor.moveToFirst()) {
        String idstr = cursor.getString(cursor.getColumnIndexOrThrow(Artists.IDSTR));
        String artistName = cursor.getString(cursor.getColumnIndexOrThrow(Artists.NAME));
        Uri uri = BASE_URI.buildUpon().appendPath(idstr).build();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, artistName);
        intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(uri));
        intent = Intent.createChooser(intent, null);
        startActivity(intent);/*from   ww  w. j  a  v  a 2 s .  c o m*/
    }
}

From source file:com.pdftron.pdf.utils.Utils.java

public static String getUriColumnInfo(Context context, Uri contentUri, String column) {
    Cursor cursor = null;
    try {/*from   w  ww.  j  a  va2  s. c  o  m*/
        String[] proj = { column };
        cursor = context.getContentResolver().query(contentUri, proj, // Which columns to return
                null, // WHERE clause; which rows to return (all rows)
                null, // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        if (null != cursor) {
            cursor.moveToFirst();
            int column_index = cursor.getColumnIndexOrThrow(proj[0]);
            return cursor.getString(column_index);
        }
        return "";
    } catch (Exception e) {
        AnalyticsHandlerAdapter.getInstance().sendException(e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return "";
}