限制在一个ContentResolver.query行数()函数函数、行数、ContentResolver、query

2023-09-06 01:35:56 作者:嫑忈

有没有一种方法,以返回的行数限制在一个游标? 我有一个约4000联系人的电话,我只需要其中的一部分。

这是在code我使用

  DB =新dBHelper(本);
        ContentResolver的CR = getContentResolver();
        光标光标;

        光标= cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,联系人姓名+ASC);
        Log.i(TAG,CLASSNAME +得到联系人条目);
        对于(INT它= 0;它与小于100;它++){// cursor.getCount()
            Log.i(TAG,CLASSNAME +获得字符串);
            串mytimes_contacted = cursor.getString(cursor.getColumnIndex(dBHelper.times_contacted));
            Log.i(TAG,CLASSNAME +从字符串做);
        }
 

日志我越来越为

  I /检查(11506):[ContactsPicker]有联系人条目
I /检查(11506):[ContactsPicker]得到第一个字符串
D / AndroidRuntime(11506):关闭虚拟机
W / dalvikvm(11506):主题ID = 1:螺纹退出与未捕获的异常(组= 0x2aac8578)
D / dalvikvm(11541):GC_CONCURRENT释放923K,46%免费4000K / 7303K,外部1685K / 2133K,暂停1毫秒+ 8ms的
E / AndroidRuntime(11506):致命异常:主要
E / AndroidRuntime(11506):java.lang.RuntimeException的:无法启动的活动ComponentInfo {~~我的包的名字〜}:android.database.CursorIndexOutOfBoundsException:指数-1要求,以3537尺寸
 
即使用了 https 也不要通过 query strings 传敏感数据

解决方案

要限制你的光标试结果的数量:

 光标= cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,联系人姓名+LIMIT 100);
而(cursor.moveToNext()){
    //一些聪明
}
 

Is there a way to limit the number of returned rows to a cursor? I have a phone with about 4000 contacts, I just need some of them.

this is the code i'm using

        db = new dBHelper(this);
        ContentResolver cr = getContentResolver();
        Cursor cursor;

        cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, ContactName + " ASC");
        Log.i(TAG, CLASSNAME + " got contacts entries");
        for (int it = 0; it <100 ; it++){//cursor.getCount()
            Log.i(TAG, CLASSNAME + " getting string");
            String mytimes_contacted = cursor.getString(cursor.getColumnIndex(dBHelper.times_contacted)); 
            Log.i(TAG, CLASSNAME + " done from the string");
        }

the Log i'm getting is

I/Check(11506): [ContactsPicker] got contacts entries
I/Check(11506): [ContactsPicker] getting first string
D/AndroidRuntime(11506): Shutting down VM
W/dalvikvm(11506): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
D/dalvikvm(11541): GC_CONCURRENT freed 923K, 46% free 4000K/7303K, external 1685K/2133K, paused 1ms+8ms
E/AndroidRuntime(11506): FATAL EXCEPTION: main
E/AndroidRuntime(11506): java.lang.RuntimeException: Unable to start activity ComponentInfo{~~my package name~~}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3537

解决方案

To limit the number of results in your cursor try:

cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, ContactName + " LIMIT 100");
while(cursor.moveToNext()) {
    // something clever
}