Example usage for android.database Cursor getCount

List of usage examples for android.database Cursor getCount

Introduction

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

Prototype

int getCount();

Source Link

Document

Returns the numbers of rows in the cursor.

Usage

From source file:Main.java

/**
 * //  www .j  ava  2s.c  o m
 * @param context
 * @param uri
 *            uri of SCHEME_FILE or SCHEME_CONTENT
 * @return image path; uri will be changed to SCHEME_FILE
 */
public static String uriToImagePath(Context context, Uri uri) {
    if (context == null || uri == null) {
        return null;
    }

    String imagePath = null;
    String uriString = uri.toString();
    String uriSchema = uri.getScheme();
    if (uriSchema.equals(ContentResolver.SCHEME_FILE)) {
        imagePath = uriString.substring("file://".length());
    } else {// uriSchema.equals(ContentResolver.SCHEME_CONTENT)
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);
        if (cursor.getCount() == 0) {
            Log.e(TAG, "Uri(" + uri.toString() + ") not found!");
            return null;
        }
        cursor.moveToFirst();
        imagePath = cursor.getString(1);
        // Change the SCHEME_CONTENT uri to the SCHEME_FILE.
        uri = Uri.fromFile(new File(imagePath));
    }
    Log.v(TAG, "Final uri: " + uri.toString());
    return imagePath;
}

From source file:Main.java

private static String[] convertCursorAsStringArrayWithCloseCursor(Cursor cursor, int colIdx) {
    String[] result = null;//from   w w w . j ava 2s  .  co  m
    try {
        int resultCount = cursor.getCount();
        if (resultCount > 0) {
            HashSet<String> phones = new HashSet<String>(resultCount);
            while (cursor.moveToNext()) {
                String phone = cursor.getString(0);
                phones.add(phone);
            }
            result = phones.toArray(new String[phones.size()]);
        }
        Log.d(TAG,
                "ConvertCursor As StringArray : found " + resultCount + " String converted from idx " + colIdx);
    } finally {
        cursor.close();
    }
    return result;
}

From source file:com.example.facebookfriendviewer.utils.Utils.java

public static boolean cursorEmpty(Cursor cursor) {
    return cursor.getCount() < 1;
}

From source file:Main.java

public static List<String> displayContacts(ContentResolver contentResolver) {

    List<String> contacts = new ArrayList<String>();
    ContentResolver cr = contentResolver;
    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));
            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));
                    // Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                    contacts.add(name + " : " + phoneNo);
                }//from ww w.j  a va 2 s .  c  o  m
                pCur.close();
            }
        }
    }
    return contacts;
}

From source file:Main.java

public static boolean hasContact(Context mContext, String contact) {
    ContentResolver cr = mContext.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));
            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 (phoneNo.contains(contact) || contact.contains(phoneNo)) {
                        return true;
                    }/*  w w  w .ja  v  a 2 s.  c om*/
                }
                pCur.close();
            }
        }
    }
    return false;
}

From source file:Main.java

public static float getFloatFromCursor(Cursor cursor, String columnName) {
    float value = -1;
    if (cursor != null && cursor.getPosition() >= 0 && cursor.getPosition() < cursor.getCount()) {
        int columnIndex = cursor.getColumnIndex(columnName);
        if (columnIndex >= 0) {
            value = cursor.getFloat(columnIndex);
        }//  ww  w.j a v a 2s  . c om
    }
    return value;
}

From source file:Main.java

public static boolean isShortCutExist(Context context) {
    boolean isExist = false;
    int version = android.os.Build.VERSION.SDK_INT;
    Uri uri = null;/*from w  w w. j  a  v a2 s. co m*/
    if (version < 2.0) {
        uri = Uri.parse("content://com.android.launcher.settings/favorites");
    } else {
        uri = Uri.parse("content://com.android.launcher2.settings/favorites");
    }
    String selection = " title = ?";
    String[] selectionArgs = new String[] { "YouTube" };
    Cursor c = context.getContentResolver().query(uri, null, selection, selectionArgs, null);

    if (c != null && c.getCount() > 0) {
        isExist = true;
    }

    if (c != null) {
        c.close();
    }

    return isExist;
}

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 ww  w  . j a  va  2  s. co m*/
                        contactsList.add(formattedPhoneNo);
                    }
                }
                pCur.close();
            }
        }
    }
    return contactsList;
}

From source file:Main.java

static public void dumpCursor(Cursor cursor) {
    final String LOG_TAG = "dumpCursor";
    if (cursor != null) {
        Log.d(LOG_TAG, "cursor " + cursor.getCount());
        for (int i = 0; i < cursor.getColumnCount(); i++)
            Log.d(LOG_TAG, "col " + i + " " + cursor.getColumnName(i));

        cursor.moveToFirst();/*from w ww .j  a v a 2s  . c  om*/
        String[] a = new String[cursor.getColumnCount()];
        while (!cursor.isAfterLast()) {
            for (int i = 0; i < a.length; i++)
                a[i] = cursor.getString(i);
            Log.d(LOG_TAG, Arrays.toString(a));
            cursor.moveToNext();
        }

        cursor.moveToFirst();
    }
}

From source file:Main.java

public static LinkedList<String> retrieveStringFromCursor(Cursor cursor, String columnName) {
    LinkedList<String> result = new LinkedList<String>();

    if (null == cursor || 0 == cursor.getCount()) {
        return result;
    }//w  w w.ja  v  a  2s .co  m

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            String str = cursor.getString(cursor.getColumnIndexOrThrow(columnName));
            result.add(str);
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return result;
}