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:im.delight.android.commons.Social.java

/**
 * Returns a list of email addresses for the contact with the given lookup ID
 *
 * @param contactLookupId a contact's lookup ID to get the email addresses for
 * @param context a context reference/*w w  w.  j a  v a  2s .com*/
 * @return CharSequence[] a list of all email addresses for the given contact or `null`
 */
public static CharSequence[] getContactEmail(final String contactLookupId, final Context context) {
    final Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    final String[] projection = new String[] { ContactsContract.CommonDataKinds.Email.DATA };
    final String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    final String[] selectionArgs = new String[] { contactLookupId };
    final String sortOrder = null;

    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);

    String email;
    if (result != null) {
        if (result.getCount() > 0) {
            final CharSequence[] res = new CharSequence[result.getCount()];

            int i = 0;
            while (result.moveToNext()) {
                email = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                if (email != null) {
                    res[i] = email;
                    i++;
                }
            }

            result.close();

            return res;
        } else {
            result.close();

            return null;
        }
    } else {
        return null;
    }
}

From source file:curso.android.DAO.PerguntaDAO.java

public static List<Pergunta> readAll() {
    Cursor cursor = null;
    try {/*from  ww  w .  j  av a2  s  .  c o  m*/
        List<Pergunta> all = new ArrayList<Pergunta>();

        cursor = Const.db.rawQuery("SELECT * FROM pergunta WHERE user_id <> " + Const.config.idUser, null);

        if (cursor.getCount() > 0) {
            int idIndex = cursor.getColumnIndex("ask_id");
            int userIndex = cursor.getColumnIndex("user_id");
            int perguntaIndex = cursor.getColumnIndex("ask_pergunta");
            int nomeIndex = cursor.getColumnIndex("user_name");
            int statusIndex = cursor.getColumnIndex("status");
            cursor.moveToFirst();
            do {
                Long id = Long.valueOf(cursor.getInt(idIndex));
                Long user = Long.valueOf(cursor.getString(userIndex));
                String pergunta = cursor.getString(perguntaIndex);
                String user_name = cursor.getString(nomeIndex);
                String status = cursor.getString(statusIndex);

                Pergunta ask = new Pergunta();
                ask.setAsk_id(id);
                ask.setUser_id(user);
                ask.setAsk_pergunta(pergunta);
                ask.setUser_name(user_name);
                ask.setStatus(Integer.valueOf(status) == 1);

                all.add(ask);

                cursor.moveToNext();
            } while (!cursor.isAfterLast());
        }

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

From source file:curso.android.DAO.PerguntaDAO.java

public static List<Pergunta> getPerguntasUsuario() {
    Cursor cursor = null;
    try {/*w  w  w.j  a va 2  s  . c  o m*/
        List<Pergunta> questions = new ArrayList<Pergunta>();

        cursor = Const.db.rawQuery("SELECT * FROM pergunta WHERE user_id =" + Const.config.idUser, null);

        if (cursor.getCount() > 0) {
            int idIndex = cursor.getColumnIndex("ask_id");
            int userIndex = cursor.getColumnIndex("user_id");
            int perguntaIndex = cursor.getColumnIndex("ask_pergunta");
            int nameIndex = cursor.getColumnIndex("user_name");
            int statusIndex = cursor.getColumnIndex("status");

            cursor.moveToFirst();
            do {
                Long id = Long.valueOf(cursor.getInt(idIndex));
                Long user = Long.valueOf(cursor.getString(userIndex));
                String pergunta = cursor.getString(perguntaIndex);
                String user_name = cursor.getString(nameIndex);
                String status = cursor.getString(statusIndex);

                Pergunta ask = new Pergunta();
                ask.setAsk_id(id);
                ask.setUser_id(user);
                ask.setAsk_pergunta(pergunta);
                ask.setUser_name(user_name);
                ask.setStatus(Integer.valueOf(status) == 1);

                questions.add(ask);

                cursor.moveToNext();
            } while (!cursor.isAfterLast());
        }

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

From source file:im.delight.android.commons.Social.java

/**
 * Whether the specified person is known on the current device or not
 *
 * @param context a context reference//from  w ww  .  j ava  2s . c  o  m
 * @param phoneNumber the phone number to look up
 * @return whether the phone number is in the local address book or not
 */
public static boolean isPersonKnown(final Context context, final String phoneNumber) {
    try {
        final Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
        final Cursor phoneEntries = context.getContentResolver().query(phoneUri,
                new String[] { android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

        return phoneEntries.getCount() > 0;
    } catch (Exception e) {
        return false;
    }
}

From source file:at.diamonddogs.util.Utils.java

/**
 * Checks a cursor for validity//from w ww.  ja v  a2s . co m
 *
 * @param c the {@link Cursor} to check
 * @return <code>true</code> if the cursor is not <code>null</code>, not
 * closed and not empty, <code>false</code> otherwise
 */
public static boolean checkCursor(Cursor c) {
    if (c == null || c.isClosed()) {
        return false;
    }

    if (c.getCount() <= 0) {
        c.close();
        return false;
    }
    return true;
}

From source file:edu.stanford.mobisocial.dungbeetle.ImageGalleryActivity.java

private static int binarySearch(Cursor c, long id, int colId) {
    long test;/*w  ww .  j  av a2s. c  o  m*/
    int first = 0;
    int max = c.getCount();
    while (first < max) {
        int mid = (first + max) / 2;
        c.moveToPosition(mid);
        test = c.getLong(colId);
        if (id > test) {
            max = mid;
        } else if (id < test) {
            first = mid + 1;
        } else {
            return mid;
        }
    }
    return 0;
}

From source file:com.rp.justcast.video.VideoProvider.java

public static List<MediaInfo> buildMedia() throws JSONException {

    if (null != mediaList) {
        return mediaList;
    }/*from   w w w  . j a  va  2  s . com*/

    String[] columns = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
            MediaStore.Video.Media.DURATION };
    String orderBy = MediaStore.Images.Media.DATE_TAKEN + " desc";
    Cursor videoCursor = null;
    try {
        videoCursor = JustCast.getmAppContext().getContentResolver()
                .query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
        videoCursor.moveToFirst();
        long fileId = videoCursor.getLong(videoCursor.getColumnIndex(MediaStore.Video.Media._ID));
        Log.w(TAG, "Building Media");
        Log.w(TAG, "Video Count" + videoCursor.getCount());
        int count = videoCursor.getCount();
        Log.d(TAG, "Count of images" + count);
        mediaList = new ArrayList<MediaInfo>();
        for (int i = 0; i < count; i++) {
            videoCursor.moveToPosition(i);
            int dataColumnIndex = videoCursor.getColumnIndex(MediaStore.Video.Media.DATA);
            int titleIndex = videoCursor.getColumnIndex(MediaStore.Video.Media.TITLE);
            Log.w(TAG, "Video added" + videoCursor.getString(dataColumnIndex));
            String path = videoCursor.getString(dataColumnIndex);
            String title = videoCursor.getString(titleIndex);
            MediaMetadata movieMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
            movieMetadata.putString("VIDEO_PATH", path);
            movieMetadata.putString(MediaMetadata.KEY_SUBTITLE, title);
            movieMetadata.putString(MediaMetadata.KEY_TITLE, title);
            movieMetadata.putString(MediaMetadata.KEY_STUDIO, title);
            path = JustCast.addJustCastServerParam(path);
            MediaInfo mediaInfo = new MediaInfo.Builder(path).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
                    .setContentType(getMediaType()).setMetadata(movieMetadata).build();
            mediaList.add(mediaInfo);
        }
    } finally {
        videoCursor.close();
    }
    return mediaList;
}

From source file:Main.java

public static LinkedHashMap<String, Uri> GetMatchedContactsList(Context context, String searchTerm) {
    LinkedHashMap<String, Uri> contactList = new LinkedHashMap<String, Uri>();
    ContentResolver cr = context.getContentResolver();
    String columns[] = { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
    Cursor cur;
    if (searchTerm == null) {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
    } else {//  ww w  .j a  v a2 s . c om
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns,
                ContactsContract.Contacts.DISPLAY_NAME + " LIKE "
                        + DatabaseUtils.sqlEscapeString("%" + searchTerm + "%"),
                null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    }
    if (cur != null) {
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String photoURI = cur
                        .getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                if (photoURI != null) {
                    Uri thumbUri = Uri.parse(photoURI);
                    contactList.put(name, thumbUri);
                }
            }
        }
        cur.close();
    }
    return contactList;
}

From source file:com.example.igorklimov.popularmoviesdemo.helpers.Utility.java

public static boolean isFavorite(Cursor cursor, Context context) {
    String title = getTitle(cursor);
    Cursor query = context.getContentResolver().query(FavoriteMovie.CONTENT_URI, null,
            MovieContract.COLUMN_TITLE + "=?", new String[] { title }, null);
    return query.getCount() != 0;
}

From source file:com.google.samples.apps.topeka.persistence.TopekaDatabaseHelper.java

private static List<Category> loadCategories(Context context) {
    Cursor data = TopekaDatabaseHelper.getCategoryCursor(context);
    List<Category> tmpCategories = new ArrayList<>(data.getCount());
    final SQLiteDatabase readableDatabase = TopekaDatabaseHelper.getReadableDatabase(context);
    do {//from   ww w. j a  v  a  2s . c  o  m
        final Category category = getCategory(data, readableDatabase);
        tmpCategories.add(category);
    } while (data.moveToNext());
    return tmpCategories;
}