0

お気に入りのプロモーション オブジェクトを配列に入れるためにチェックを行っています。私はこれをやろうとしました(SQLiteデータベースからテーブルプロモーションを制御するPromotionsBDDクラスから):

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つある場合、追加した後、次にインデックス2にし、ループして再度追加します。アイテムが 2 つありますね。

一度追加した項目が 3 つある場合、それは最後ではないため、再度ループすることはありません。あなたは1つになります。

代わりに、次のようなことを試してください ( 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 に答える