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 String getContactName(Context context, String number) {

    String name = null;/*from  w  ww .  jav a  2s.c  om*/

    // 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 getSdCardPath(ContentResolver cr, Uri uri, String defaultPath) {
    ArrayList files = new ArrayList();
    if (uri != null) {
        Cursor mCursor = cr.query(uri, null, null, null, null);
        if (mCursor != null) {
            while (mCursor.moveToNext()) {
                String path = mCursor.getString(mCursor.getColumnIndexOrThrow("_data"));
                if (!path.contains(defaultPath)) {
                    String parentFolder = (new File(path)).getParent();
                    if (!files.contains(parentFolder)) {
                        files.add(parentFolder);
                    }/*from   w  ww. j  av a 2s . c om*/
                }
            }
        }
    }

    return getCommonSubStringFromList(files);
}

From source file:Main.java

/**
 * Gets the column data, returns a String
 *
 * @param cursor//from   ww w . jav  a  2 s. c  o  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 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 www .j a  va  2s  .c  om
                        contactsList.add(formattedPhoneNo);
                    }
                }
                pCur.close();
            }
        }
    }
    return contactsList;
}

From source file:Main.java

public static String get(Context context, String key) {
    String temp = null;/*from  w  ww  .  ja  v  a 2 s. c om*/
    Cursor cur = null;
    try {
        cur = context.getContentResolver().query(CONTENT_URI, null, "key='" + key + "'", null, null);
        if (null != cur && cur.moveToFirst())
            temp = cur.getString(1);

    } catch (Exception e) {
        Log.e("AspShareUtil", "Error while get", e);
    } finally {
        if (cur != null)
            cur.close();
    }
    return temp;
}

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.j  a v  a  2 s  .  c  om
        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 String getApnProxy(Context context) {
    Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
    c.moveToFirst();//from  w  ww  .j a v a 2s. co  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

final static public String GetContactNameByID(long contact_id, ContentResolver resolver) {
    if (resolver != null) {
        final Uri uri = Data.CONTENT_URI;

        final String[] columns = { ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME
                //,ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME
                /*Data._ID,*/
                //Data.CONTACT_ID, 
                //Data.LOOKUP_KEY
        };/*from  w  w w. j av  a2s .  c  o m*/
        final String where = Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                + StructuredName.CONTENT_ITEM_TYPE + "'";
        final String[] where_arg = { String.valueOf(contact_id) };

        Cursor cur = resolver.query(uri, columns, where, where_arg, null);
        if (cur.moveToFirst()) {
            String result = "";
            final String family_name = cur.getString(0);
            if (family_name != null)
                result += family_name;
            final String given_name = cur.getString(1);
            if (given_name != null)
                result += " " + given_name;
            final String middle_name = cur.getString(2);
            if (middle_name != null)
                result += " " + middle_name;
            //return String.format("%s %s %s",family_name,given_name,middle_name);
            return result;
        } //if(cur.moveToFirst())
    } //if(resolver!=null)
    return null;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;/*from   w ww. ja v a  2  s  . com*/

    CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        result = cursor.getString(column_index);
    }
    return result;
}

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();// w  ww.  jav  a2 s.  c o  m
    if (c.isAfterLast()) {
        c.close();
        return null;
    }

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