0

我正在检查以将我最喜欢的自己的 Promotions 对象放入数组中。我试图这样做(来自我的 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 ..

4

2 回答 2

0

这是执行此操作的方法:

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;
于 2012-08-12T12:49:47.390 回答
0

你正在做(伪代码):

first
        add(current)
        next
    repeat if last

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

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

相反,请尝试类似(您不需要 isFirst 检查并且您需要 while not last):

if (objCursor == null)
    return array; // empty

objCursor.moveToFirst();
do
{
    // read data
    array.Add(promo);

} while (!objCursor.isLast())
于 2012-08-12T12:21:28.093 回答