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:sg.macbuntu.android.pushcontacts.SmsReceiver.java

private static String getNameFromPhoneNumber(Context context, String phone) {
    Cursor cursor = context.getContentResolver().query(
            Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, phone),
            new String[] { Contacts.Phones.NAME }, null, null, null);
    if (cursor != null) {
        try {/*ww  w. j ava  2  s.  c om*/
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                String name = cursor.getString(0);
                Log.e("PUSH_CONTACTS", "Pushed name: " + name);
                return name;
            }
        } finally {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Return text content of clipboard as individual lines 
 * @param ctx//from ww w .ja  va  2s. c  o m
 * @return
 */
@SuppressLint("NewApi")
private static ArrayList<String> getTextLines(Context ctx) {

    String EOL = "\\r?\\n|\\r";

    if (checkForText(ctx)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

            // Gets the clipboard as text.
            CharSequence cs = item.getText();
            if (cs == null) { // item might be an URI
                Uri pasteUri = item.getUri();
                if (pasteUri != null) { // FIXME untested
                    try {
                        Log.d("ClipboardUtils", "Clipboard contains an uri");
                        ContentResolver cr = ctx.getContentResolver();
                        String uriMimeType = cr.getType(pasteUri);
                        //               pasteData = resolveUri(pasteUri);
                        // If the return value is not null, the Uri is a content Uri
                        if (uriMimeType != null) {

                            // Does the content provider offer a MIME type that the current application can use?
                            if (uriMimeType.equals(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

                                // Get the data from the content provider.
                                Cursor pasteCursor = cr.query(pasteUri, null, null, null, null);

                                // If the Cursor contains data, move to the first record
                                if (pasteCursor != null) {
                                    if (pasteCursor.moveToFirst()) {
                                        String pasteData = pasteCursor.getString(0);
                                        return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
                                    }
                                    // close the Cursor
                                    pasteCursor.close();
                                }
                            }
                        }
                    } catch (Exception e) { // FIXME given that the above is unteted, cath all here
                        Log.e("ClipboardUtils", "Resolving URI failed " + e);
                        e.printStackTrace();
                        return null;
                    }
                }
            } else {
                Log.d("ClipboardUtils", "Clipboard contains text");
                String pasteData = cs.toString();
                return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
            }
        } else {
            // Gets the clipboard as text.
            @SuppressWarnings("deprecation")
            CharSequence cs = oldClipboard.getText();
            if (cs != null) {
                String pasteData = cs.toString();
                if (pasteData != null) { // should always be the case
                    return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
                }
            }
        }
        Log.e("ClipboardUtils", "Clipboard contains an invalid data type");
    }
    return null;
}

From source file:br.com.bea.androidtools.api.model.EntityUtils.java

public static final Object convert(final Field field, final Cursor cursor) throws Exception {
    if (field.getType().equals(Long.class))
        return cursor.getLong(cursor.getColumnIndex(field.getAnnotation(Column.class).name()));
    if (field.getType().equals(Integer.class))
        return cursor.getInt(cursor.getColumnIndex(field.getAnnotation(Column.class).name()));
    if (field.getType().equals(Date.class))
        return DATE_FORMAT
                .parse(cursor.getString(cursor.getColumnIndex(field.getAnnotation(Column.class).name())));
    return cursor.getString(cursor.getColumnIndex(field.getAnnotation(Column.class).name()));
}

From source file:Main.java

public static Map<Integer, List> getProvince(File file) {

    String sql = "select provinceid ,province from province ";
    SQLiteDatabase db = null;/*w  w w .ja  v a  2 s . c  o m*/
    Cursor c = null;
    Map<Integer, List> provinceData = new HashMap<Integer, List>();
    //List provinceList = null;
    try {
        db = SQLiteDatabase.openOrCreateDatabase(file, null);
        c = db.rawQuery(sql, null);
        List provinceList1 = new ArrayList();
        List provinceList2 = new ArrayList();
        while (c.moveToNext()) {
            Map provinceMap = new HashMap();
            provinceMap.put(c.getString(1), c.getInt(0));
            provinceList1.add(provinceMap);
            provinceList2.add(c.getString(1));
        }
        provinceData.put(0, provinceList1);
        provinceData.put(1, provinceList2);
    } catch (Exception e) {
        Log.d("WineStock", "getProvince:" + e.getMessage());
    } finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return provinceData;
}

From source file:Main.java

/**
 * Get the value of the data column for this Uri . This is useful for
 * MediaStore Uris , and other file - based ContentProviders.
 * /*from  w  ww  . j a  v a  2  s.  c  o  m*/
 * @param context
 *            The context.
 * @param uri
 *            The Uri to query.
 * @param selection
 *            (Optional) Filter used in the query.
 * @param selectionArgs
 *            (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = MediaColumns.DATA;
    final String[] projection = { column };
    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context The context./* ww  w. j  a  v  a  2s  . co  m*/
 * @param uri The Uri to query.
 * @param selection (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

public static String checkNull(Context context, int lastImageId, File fileCapture) {
    final String[] imageColumns = { Images.Media._ID, Images.Media.DATA };
    final String imageOrderBy = Images.Media._ID + " DESC";
    final String imageWhere = Images.Media._ID + ">?";
    final String[] imageArguments = { Integer.toString(lastImageId) };

    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = contentResolver.query(Images.Media.EXTERNAL_CONTENT_URI, imageColumns, imageWhere,
            imageArguments, imageOrderBy);
    if (cursor == null)
        return null;

    String newpath = null;/* ww w.  j a  v a  2s  .  c om*/
    if (cursor.getCount() >= 2) {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex(Images.Media._ID));
            String data = cursor.getString(cursor.getColumnIndex(Images.Media.DATA));
            if (data.equals(fileCapture.getPath())) {
                int rows = contentResolver.delete(Images.Media.EXTERNAL_CONTENT_URI, Images.Media._ID + "=?",
                        new String[] { Long.toString(id) });
                boolean ok = fileCapture.delete();

            } else {
                newpath = data;
            }
        }
    } else {
        newpath = fileCapture.getPath();
        Log.e("MediaUtils", "Not found duplicate.");
    }

    cursor.close();
    return newpath;
}

From source file:Main.java

public static LinkedList<Pair<Integer, String>> retrieveIntegerStringPairFromCursor(Cursor cursor,
        String integerColumnName, String stringColumnName) {
    LinkedList<Pair<Integer, String>> result = new LinkedList<Pair<Integer, String>>();

    if (null == cursor || 0 == cursor.getCount()) {
        return result;
    }//from   ww  w.j  av a 2s. com

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            Integer integerVal = cursor.getInt(cursor.getColumnIndex(integerColumnName));
            String stringVal = cursor.getString(cursor.getColumnIndexOrThrow(stringColumnName));
            result.add(new Pair(integerVal, stringVal));
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return result;
}

From source file:Main.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context       The context.// ww w .  jav a2s.co  m
 * @param uri           The Uri to query.
 * @param selection     (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context       The context.//from w ww  . j a v a 2  s  .c o m
 * @param uri           The Uri to query.
 * @param selection     (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {

        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {

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