游标返回最多 2 行



我正在做一个检查,以便将我最喜欢的自己的促销对象放入数组中。我尝试这样做(从我的 PromotionsBDD 类,它从我的 SQLite 数据库控制我的表促销):

public ArrayList<Promotion> getListPromotionsFavorites()
    {
        String[] columns = new String[] {"ID", "RATING", "TITLE", "COMPANY_ID","AVAILABLEDATE", "DESCRIPTION", "SETONFAVORITE"};
        Cursor objCursor = bdd.query(TABLE_PROMOTIONS, columns,"SETONFAVORITE = ?",new String[]{"true"},null,null,null,null);
        int id = objCursor.getColumnIndex("ID");
        int rating = objCursor.getColumnIndex("RATING");
        int title = objCursor.getColumnIndex("TITLE");
        int companyid = objCursor.getColumnIndex("COMPANY_ID");
        int availabledate = objCursor.getColumnIndex("AVAILABLEDATE");
        int description = objCursor.getColumnIndex("DESCRIPTION");
        int setonfavorite = objCursor.getColumnIndex("SETONFAVORITE");
        ArrayList<Promotion> promoFavoriteArray = new ArrayList<Promotion>();
        objCursor.moveToFirst();// position sur la première ligne
        if (objCursor != null) 
        {
            if (objCursor.isFirst())
            {
                do 
                {
                    String resultid = objCursor.getString(id);
                    String resultrating = objCursor.getString(rating);
                    String resultitle = objCursor.getString(title);
                    int resultcompanyid = objCursor.getInt(companyid);
                    String resultavailbledate = objCursor.getString(availabledate);
                    String resultdescription = objCursor.getString(description);
                    String resultsetonfavorite = objCursor.getString(setonfavorite);
                    Promotion promo = new Promotion(resultid, resultrating, resultitle, resultcompanyid,resultavailbledate,resultdescription, resultsetonfavorite);
                    promoFavoriteArray.add(promo);
                    objCursor.moveToNext();//positionnement sur le suivant
                }
                while(objCursor.isLast());
            }
        }
        objCursor.deactivate();
        objCursor.close();
    return promoFavoriteArray;
}

使用我的 promoFavoriteArray 使用 1 或 2 个条目可以正常工作,但我将 3 个条目放入此数组,它返回一个 1 到 3 的数组。

编辑:我试过这个:

ArrayList<Promotion> promoFavoriteArray = new ArrayList<Promotion>();
            objCursor.moveToFirst();
            if(objCursor != null)
            {
                do 
                {
                        String resultid = objCursor.getString(id);
                        String resultrating = objCursor.getString(rating);
                        String resultitle = objCursor.getString(title);
                        int resultcompanyid = objCursor.getInt(companyid);
                        String resultavailbledate = objCursor.getString(availabledate);
                        String resultdescription = objCursor.getString(description);
                        String resultsetonfavorite = objCursor.getString(setonfavorite);
                        Promotion promo = new Promotion(resultid, resultrating, resultitle, resultcompanyid,resultavailbledate,resultdescription, resultsetonfavorite);
                        promoFavoriteArray.add(promo);
                        objCursor.moveToNext();//positionnement sur le suivant
                }
                while(!objCursor.isLast());
            }
        objCursor.deactivate();
        objCursor.close();
        return promoFavoriteArray;

但我得到了CursorIndexOutOfBoundException。

你正在做(伪代码):

first
        add(current)
        next
    repeat if last

如果有 2 个项目,则在添加后,下一个使其成为索引 2,您循环并再次添加。 你有两个项目,太好了。

如果您添加一次 3 个项目,则它不是最后一个,因此不会再次循环。 你最终会得到一个。

相反,尝试类似的东西(你不需要isFirst检查,你需要而不是最后):

if (objCursor == null)
    return array; // empty
objCursor.moveToFirst();
do
{
    // read data
    array.Add(promo);
} while (!objCursor.isLast())

这是执行此操作的方法:

ArrayList<Promotion> promoFavoriteArray = new ArrayList<Promotion>();
        boolean hasMore = objCursor.moveToFirst();
        while(hasMore)
        {
                String resultid = objCursor.getString(id);
                String resultrating = objCursor.getString(rating);
                String resultitle = objCursor.getString(title);
                int resultcompanyid = objCursor.getInt(companyid);
                String resultavailbledate = objCursor.getString(availabledate);
                String resultdescription = objCursor.getString(description);
                String resultsetonfavorite = objCursor.getString(setonfavorite);
                Promotion promo = new Promotion(resultid, resultrating, resultitle, resultcompanyid,resultavailbledate,
                        resultdescription, resultsetonfavorite);
                promoFavoriteArray.add(promo);
                hasMore = objCursor.moveToNext();
        }
    objCursor.deactivate();
    objCursor.close();
    return promoFavoriteArray;

最新更新