Skip to content Skip to sidebar Skip to footer

Limit Number Of Rows In Cursor Query

I need to get contacts from an android phone. so I am using this code: Cursor phones = getActivity().getContentResolver().query(Phone.CONTENT_URI, new String[] { Phone.NUMBER,

Solution 1:

There is no limit in Android API, but you can apply it in your code like this:

Cursor phones = getActivity().getContentResolver().query(Phone.CONTENT_URI,
    newString[] { Phone.NUMBER, Phone.TYPE },
    " DISPLAY_NAME = ?", newString[] { displayName }, null);
for (int i = 0; i < 50 && phones.moveToNext(); ++i) {
   // do work here
}
phones.close();

Solution 2:

Order by id DESC Limit 1:

db.query("table", null, "column=?", newString[]{"value"}, null, null, "id DESC", "50");

Solution 3:

If you have the DB and the table name you can use that, but Content providers don't have a limit argument in their query statement.

mDb = newDBHelper (this);

Cursor c = mDB.getReadableDatase().query(
       <Phone_Table_Name>, 
       newString[] { Phone.NUMBER, Phone.TYPE }, 
       "DISPLAY_NAME = ?", 
       newString[] { displayName }, null), 
       null, 
       null, 
       null, 
       "50");

Post a Comment for "Limit Number Of Rows In Cursor Query"