Example usage for android.database Cursor getColumnIndex

List of usage examples for android.database Cursor getColumnIndex

Introduction

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

Prototype

int getColumnIndex(String columnName);

Source Link

Document

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

Usage

From source file:Main.java

public static String getContactName(Context context, String number) {

    String name = null;/*  w ww .  j  a v  a  2s.  com*/

    // define the columns I want the query to return
    String[] projection = new String[] { Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
            ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
            : ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.PhoneLookup._ID };

    // encode the phone number and build the filter URI
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

    // query time
    Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
            Log.v("getContactName", "Started uploadcontactphoto: Contact Found @ " + number);
            Log.v("getContactName", "Started uploadcontactphoto: Contact name  = " + name);
        } else {
            Log.v("getContactName", "Contact Not Found @ " + number);
        }
        cursor.close();
    }
    return name;
}

From source file:Main.java

public static String getApnProxy(Context context) {
    Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
    c.moveToFirst();//from   ww  w.  jav  a  2 s.  c o  m
    if (c.isAfterLast()) {
        c.close();
        return null;
    }
    String strResult = c.getString(c.getColumnIndex(APN_PROP_PROXY));
    c.close();
    return strResult;
}

From source file:Main.java

/**
 * Handles V19 and up uri's// w w w .  j  a  v  a  2s . c o  m
 * @param context
 * @param contentUri
 * @return path
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getPathForV19AndUp(Context context, Uri contentUri) {
    String wholeID = DocumentsContract.getDocumentId(contentUri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];
    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";
    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            sel, new String[] { id }, null);

    String filePath = "";
    int columnIndex = cursor.getColumnIndex(column[0]);
    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }

    cursor.close();
    return filePath;
}

From source file:Main.java

public static HashSet<String> getUserContacts(ContentResolver cr) {
    HashSet<String> contactsList = new HashSet<String>();
    //ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            // We could store names and numbers in a dictionary if we ever needed that
            // String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(
                    cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur
                            .getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // If the number stored isn't long enough to be in our database then don't bother.
                    if (phoneNo.length() >= 10) {
                        // Use regex to remove '+', spaces, '(', and ')' and '-' form user contacts to match database format
                        String formattedPhoneNo = phoneNo.replaceAll(DELIMITERS, "");
                        // Remove the '1' in front of the number
                        if (formattedPhoneNo.charAt(0) == '1') {
                            formattedPhoneNo = formattedPhoneNo.substring(1);
                        }//from w ww  . ja v  a 2  s  . c  om
                        contactsList.add(formattedPhoneNo);
                    }
                }
                pCur.close();
            }
        }
    }
    return contactsList;
}

From source file:Main.java

/**
 * Gets the column data, returns a String
 *
 * @param cursor/*from   w ww .java2  s .co  m*/
 * @param columnName
 * @return
 */
private static String getColumnData(Cursor cursor, String columnName) {
    if (cursor == null) {
        return null;
    }
    if (isValidColumn(cursor, columnName)) {
        try {
            String str = cursor.getString(cursor.getColumnIndex(columnName));
            return str;

        } catch (Exception e) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:Main.java

public static String getApn(Context context) {
    Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
    c.moveToFirst();/*from w  w w  .  j  a  va 2 s. co  m*/
    if (c.isAfterLast()) {
        c.close();
        return null;
    }

    String strResult = c.getString(c.getColumnIndex(APN_PROP_APN));
    c.close();
    return strResult;
}

From source file:Main.java

public static Bundle getContactFromNumber(String number, Context context) {
    ContentResolver cr = context.getContentResolver();
    String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME };
    Bundle bundle = new Bundle();
    Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor cursor = cr.query(contactUri, projection, null, null, null);
    try {/*from   w w w.  ja va2 s  .  com*/
        if (cursor.moveToFirst()) {
            for (String key : projection) {
                bundle.putString(key, cursor.getString(cursor.getColumnIndex(key)));
            }
        }
        return bundle;
    } catch (Exception e) {
        return null;
    } finally {
        cursor.close();
    }

}

From source file:Main.java

public static int getRotationFromGallery(Context context, Uri imageUri) {
    String[] columns = { MediaStore.Images.Media.ORIENTATION };
    Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null);
    if (cursor == null)
        return 0;

    cursor.moveToFirst();// w w w  .  ja v  a  2 s .  c  o  m

    int orientationColumnIndex = cursor.getColumnIndex(columns[0]);
    return cursor.getInt(orientationColumnIndex);
}

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

public static Uri toGeoUri(Cursor c) {
    if (c.isNull(c.getColumnIndex(Locatable.COL_LATITUDE))
            || c.isNull(c.getColumnIndex(Locatable.COL_LONGITUDE))) {
        return null;
    }/*from www. ja va  2s .  c  o  m*/
    return Uri.parse("geo:" + c.getDouble(c.getColumnIndex(Locatable.COL_LATITUDE)) + ","
            + c.getDouble(c.getColumnIndex(Locatable.COL_LONGITUDE)));
}

From source file:Main.java

public static void CheckDwnloadStatus(DownloadManager downloadManager, Activity activity, long id) {

    // TODO Auto-generated method stub
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(id);// w  w  w . j av a  2 s.c  om
    Cursor cursor = downloadManager.query(query);
    if (cursor.moveToFirst()) {
        int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
        int status = cursor.getInt(columnIndex);
        int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
        int reason = cursor.getInt(columnReason);

        switch (status) {
        case DownloadManager.STATUS_FAILED:
            String failedReason = "";
            switch (reason) {
            case DownloadManager.ERROR_CANNOT_RESUME:
                failedReason = "ERROR_CANNOT_RESUME";
                break;
            case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                failedReason = "ERROR_DEVICE_NOT_FOUND";
                break;
            case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                failedReason = "ERROR_FILE_ALREADY_EXISTS";
                break;
            case DownloadManager.ERROR_FILE_ERROR:
                failedReason = "ERROR_FILE_ERROR";
                break;
            case DownloadManager.ERROR_HTTP_DATA_ERROR:
                failedReason = "ERROR_HTTP_DATA_ERROR";
                break;
            case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                failedReason = "ERROR_INSUFFICIENT_SPACE";
                break;
            case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                failedReason = "ERROR_TOO_MANY_REDIRECTS";
                break;
            case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                failedReason = "ERROR_UNHANDLED_HTTP_CODE";
                break;
            case DownloadManager.ERROR_UNKNOWN:
                failedReason = "ERROR_UNKNOWN";
                break;
            default:
                failedReason = "unknown reason";
                break;
            }

            Toast.makeText(activity, "FAILED: " + failedReason, Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_PAUSED:
            String pausedReason = "";

            switch (reason) {
            case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                pausedReason = "PAUSED_QUEUED_FOR_WIFI";
                break;
            case DownloadManager.PAUSED_UNKNOWN:
                pausedReason = "PAUSED_UNKNOWN";
                break;
            case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                pausedReason = "PAUSED_WAITING_FOR_NETWORK";
                break;
            case DownloadManager.PAUSED_WAITING_TO_RETRY:
                pausedReason = "PAUSED_WAITING_TO_RETRY";
                break;
            }

            Toast.makeText(activity, "PAUSED: " + pausedReason, Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_PENDING:
            Toast.makeText(activity, "PENDING", Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_RUNNING:
            Toast.makeText(activity, "RUNNING", Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_SUCCESSFUL:

            Toast.makeText(activity, "SUCCESSFUL", Toast.LENGTH_LONG).show();
            break;
        }
    }
}