29
有沒有辦法將返回的行數限制爲遊標? 我有一個約4000個聯繫人的電話,我只需要其中的一部分。限制ContentResolver.query()函數中的行數
這是我使用
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
謝謝薩姆,我完全忘記了移動光標,即使在循環內,多麼愚蠢!我會嘗試您的建議並回復給您 – user1347945
更新了限制示例,但限於前100個結果。真正的問題是你想限制遊標?只有名爲「鮑勃」的人? – Sam
嗯,錯誤確實在移動光標,但我現在已經學會了如何限制光標的好處。問題是,如何限制光標大小,以便即使數據庫的數量超過我指定的數量,我也可以獲得有限的行數...... – user1347945