【问题标题】:Cursor returning only one row光标只返回一行
【发布时间】:2012-08-12 10:34:33
【问题描述】:

我正在检查,以便将我最喜欢的自己的 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,null,null,null,null,null,null);//requete de récupération de la liste

        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())
            {
                int i = 0;
                do 
                {
                    String resultsetonfavorite = objCursor.getString(setonfavorite);
                    if (resultsetonfavorite.equals("true"))
                    {
                        i++;
                        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);

                        Promotion promo = new Promotion(resultid, resultrating, resultitle, 
                                resultcompanyid,resultavailbledate,resultdescription, resultsetonfavorite);
                        promoFavoriteArray.add(promo);
                        objCursor.moveToNext();//positionnement sur le suivant
                    }
                    else
                        i++;
                }
                while(objCursor.isLast());
            }
        }

    objCursor.deactivate();
    objCursor.close();
    return promoFavoriteArray;
}

它只返回给我一个顶行的数组.. 有人知道怎么做吗?

【问题讨论】:

  • 循环不应该以 "while (!objCursor.IsLast)" 结束吗?另外,您将光标移到哪里?
  • 这里:objCursor.moveToNext();进入“做”表达式
  • 好的 - 是的。但 '(objCursor.isLast())' 在第一次迭代时为假,除非只有一条记录。如果只有一条记录,您将进入无限循环,如果有 2 个或更多记录,它将在第一个记录之后退出。

标签: arrays sqlite arraylist cursor row


【解决方案1】:

如果要检查表中是否有记录。你可以用这个。

Cursor cursor=sqLiteDatabase.rawQuery("select * from table limit 1", null);
        if (cursor.moveToNext()) {
            return true;
        }
        return false;

它只会返回找到记录。

【讨论】:

    【解决方案2】:

    换行

    while(objCursor.isLast());
    

    while(!objCursor.isLast());
    

    【讨论】:

    • @eento - 没有进一步的评论?我的回答正确吗?如果是这样,我将不胜感激接受答案
    猜你喜欢
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    相关资源
    最近更新 更多