Code example for Cursor

Methods: closegetCountgetLongmoveToFirst

0
 
        // Count contacts found by query 
        int count = 0;
        long contactId = -1;
        try { 
            count = cursor.getCount();
            if (count == 1 && cursor.moveToFirst()) {
                // Try reading ID if only one contact returned 
                contactId = cursor.getLong(CONTACT_ID_INDEX);
            } 
        } finally { 
            cursor.close();
        } 
 
        if (count == 1 && contactId != -1) {
            // If we only found one item, jump right to viewing it 
            final Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
            final Intent viewIntent = new Intent(Intent.ACTION_VIEW, contactUri);
            startActivity(viewIntent);
            finish();