Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中迭代来自游标的特定数据计数_Android_Database_Sqlite_Loops_Cursor - Fatal编程技术网

在Android中迭代来自游标的特定数据计数

在Android中迭代来自游标的特定数据计数,android,database,sqlite,loops,cursor,Android,Database,Sqlite,Loops,Cursor,我想遍历数据库中的游标数据,我不会返回游标中的所有数据。我只需要5行随机数据,我有逻辑返回下面的所有数据,但我只想返回5行 private ArrayList<Insect> insectList(){ ArrayList<Insect> insects = new ArrayList<>(); Collections.shuffle(insects); if (cursor.moveToFirst()) {

我想遍历数据库中的游标数据,我不会返回游标中的所有数据。我只需要5行随机数据,我有逻辑返回下面的所有数据,但我只想返回5行

  private ArrayList<Insect> insectList(){


    ArrayList<Insect> insects = new ArrayList<>();
    Collections.shuffle(insects);

    if (cursor.moveToFirst()) {
        do {
            Insect insect = new Insect();
            insect.setName(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_FRIENDLYNAME)));
            insect.setScientificName(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME)));
            insect.setClassification(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME)));
            insect.setImageAsset(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_IMAGEASSET)));
            insect.setDangerLevel(Integer.parseInt(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_DANGERLEVEL))));

            insects.add(insect);
        } while (cursor.moveToNext());
    }



    return insects;
}
private ArrayList insectList(){
ArrayList昆虫=新的ArrayList();
收藏。洗牌(昆虫);
if(cursor.moveToFirst()){
做{
昆虫=新昆虫();
setName(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_FRIENDLYNAME));
昆虫.setScientificName(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME));
昆虫.setClassification(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME));
setImageAsset(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_IMAGEASSET));
setDangerLevel(Integer.parseInt(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_DANGERLEVEL));
昆虫。添加(昆虫);
}while(cursor.moveToNext());
}
返回昆虫;
}
如果(cursor.getCount()>0)进行迭代时,我可以使用哪个完美循环从该游标中仅获得五行{
if (cursor.getCount() > 0) {
    int count = 0;
    while (cursor.moveToNext()){
         if(count < 5){ 
             count++;
             Insect insect = new Insect();
        insect.setName(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_FRIENDLYNAME)));
        insect.setScientificName(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME)));
        insect.setClassification(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME)));
        insect.setImageAsset(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_IMAGEASSET)));
        insect.setDangerLevel(Integer.parseInt(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_DANGERLEVEL))));

        insects.add(insect);
         }
    }
    cursor.close();
}
整数计数=0; while(cursor.moveToNext()){ 如果(计数<5){ 计数++; 昆虫=新昆虫(); setName(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_FRIENDLYNAME)); 昆虫.setScientificName(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME)); 昆虫.setClassification(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME)); setImageAsset(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_IMAGEASSET)); setDangerLevel(Integer.parseInt(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_DANGERLEVEL)); 昆虫。添加(昆虫); } } cursor.close(); }
您确定这样做没有遗漏第一行吗?是的,使用moveToNext()函数,光标将从0移动到最后一行