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:Main.java

public static void addToCalendar(Activity context, Long beginTime, String title) {
    ContentResolver cr = context.getContentResolver();

    Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
    Long time = new Date(beginTime).getTime();
    ContentUris.appendId(builder, time - 10 * 60 * 1000);
    ContentUris.appendId(builder, time + 10 * 60 * 1000);

    String[] projection = new String[] { "title", "begin" };
    Cursor cursor = cr.query(builder.build(), projection, null, null, null);

    boolean exists = false;
    if (cursor != null) {
        while (cursor.moveToNext()) {
            if ((time == cursor.getLong(1)) && title.equals(cursor.getString(0))) {
                exists = true;/*w w w . j  a v  a2 s  .  c o  m*/
            }
        }
    }

    if (!exists) {
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", time);
        intent.putExtra("allDay", false);
        intent.putExtra("endTime", time + 60 * 60 * 1000);
        intent.putExtra("title", title);
        context.startActivity(intent);
    } else {
        Toast.makeText(context, "Event already exist!", Toast.LENGTH_LONG).show();
    }
}

From source file:disono.webmons.com.utilities.helpers.WBFile.java

/**
 * Get the real path from URI/*from ww  w  .  j a  va  2  s.c om*/
 *
 * @param activity
 * @param uri
 * @return
 */
public static String getRealPathFromURI(Activity activity, Uri uri) {
    Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
    assert cursor != null;
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

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

public static List<GeoPoint> getPath(Cursor c) {
    final ArrayList<GeoPoint> path = new ArrayList<GeoPoint>();
    final String encodedPath = c.getString(c.getColumnIndex(_PATH));
    if (encodedPath == null) {
        return new ArrayList<GeoPoint>();
    }//from w  ww . jav  a2 s  . c  o  m

    int prevI = 0;

    int i = 0;
    while (i != -1) {
        i = encodedPath.indexOf(',', i + 1);
        // handle the case for empty paths
        if (i == -1) {
            break;
        }
        final int lat = Integer.parseInt(encodedPath.substring(prevI, i));
        prevI = i + 1;
        i = encodedPath.indexOf(',', prevI);

        int end = i;
        if (i == -1) {
            end = encodedPath.length();
        }
        final int lon = Integer.parseInt(encodedPath.substring(prevI, end));
        path.add(new GeoPoint(lat, lon));
        prevI = i + 1;
    }
    return path;
}

From source file:Main.java

static String getAccessTokenFromTable(Context context, String tableName) {
    String token = null;//  w w w .  j a va  2 s.c  om
    try {
        SQLiteDatabase db = context.openOrCreateDatabase(DEPRECATED_DATABASE_NAME, 0, null);
        Cursor c = db.rawQuery(
                "SELECT " + DEPRECATED_ACCESS_TOKEN_COLUMN + " FROM " + tableName + " WHERE local_id=0", null);
        if (c.moveToFirst() && c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN) != -1) {
            token = c.getString(c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN));
        }
        c.close();
        db.close();
    } catch (SQLException e) {
        // DB doesn't exist
    }
    return token;
}

From source file:Main.java

/**
 * Handles pre V19 uri's//from  w w w . j av  a  2 s  .c o m
 * @param context
 * @param contentUri
 * @return
 */
private static String getPathForPreV19(Context context, Uri contentUri) {
    String res = null;

    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            res = cursor.getString(column_index);
        }
        cursor.close();
    }

    return res;
}

From source file:fr.mixit.android.io.JSONHandler.java

/**
 * Returns those id's from a {@link android.net.Uri} that were not found in a given set.
 */// w  w  w.  j  a  va 2s .c om
protected static HashSet<String> getLostIds(Set<String> ids, Uri uri, String[] projection, int idColumnIndex,
        ContentResolver resolver) {
    final HashSet<String> lostIds = Sets.newHashSet();

    final Cursor cursor = resolver.query(uri, projection, null, null, null);
    try {
        while (cursor.moveToNext()) {
            final String id = cursor.getString(idColumnIndex);
            if (!ids.contains(id)) {
                lostIds.add(id);
            }
        }
    } finally {
        cursor.close();
    }

    if (!lostIds.isEmpty()) {
        Log.d(TAG, "Found " + lostIds.size() + " for " + uri.toString() + " that need to be removed.");
    }

    return lostIds;
}

From source file:org.hfoss.posit.android.web.PositHttpUtils.java

public static List<NameValuePair> convertFindsForPost(String[] projection, Cursor c) {
    List<NameValuePair> finds = new ArrayList<NameValuePair>();
    c.moveToFirst();/*from  w ww  . j  a va  2 s .  c  o m*/
    try {
        for (String item : projection) {
            finds.add(new BasicNameValuePair(item, c.getString(c.getColumnIndexOrThrow(item))));
        }
    } catch (CursorIndexOutOfBoundsException e) {
        if (Utils.debug)
            Log.e(TAG, e.getMessage());
    }
    return finds;
}

From source file:Main.java

public static String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {/* w  w  w . j  a  v a  2s .c  o  m*/
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

public static String getAbsoluteImagePath(Context context, Uri uri) {
    String imagePath = "";
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (cursor.getCount() > 0 && cursor.moveToFirst()) {
            imagePath = cursor.getString(column_index);
        }//from   www.j  a v a2s.c  o m
    }

    return imagePath;
}

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

public static Set<String> getTags(ContentProviderClient cpc, Uri tagDir) throws RemoteException {
    final Set<String> tags = new HashSet<String>();

    final Cursor c = cpc.query(tagDir, TAG_PROJECTION, null, null, null);

    try {// www . j ava2  s . c  om
        final int tagNameCol = c.getColumnIndexOrThrow(Tag.COL_NAME);

        while (c.moveToNext()) {
            tags.add(c.getString(tagNameCol));
        }
    } finally {
        c.close();
    }

    return tags;
}