gpt4 book ai didi

android - 限制 ContentResolver.query() 函数中的行数

转载 作者:IT老高 更新时间:2023-10-28 21:52:30 27 4
gpt4 key购买 nike

有没有办法限制游标返回的行数?我的手机有大约 4000 个联系人,我只需要其中的一些。

这是我正在使用的代码

        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");
}

我得到的日志是

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

最佳答案

要限制光标中的结果数量,请尝试:

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

关于android - 限制 ContentResolver.query() 函数中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10390577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com