2012-08-12 172 views
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,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; 
} 

而且它只有返回我頂行的數組.. 有人知道如何做?

+0

該循環不應以「while(!objCursor.IsLast)」結束嗎?你還在哪裏推進光標? – 2012-08-12 10:38:22

+0

here:objCursor.moveToNext();進入「做」表達 – eento 2012-08-12 10:44:02

+0

好 - 是的。但是'(objCursor.isLast())'在第一次迭代中是錯誤的,除非只有一條記錄。如果只有一條記錄,您將進入無限循環,如果有2條或更多條記錄,它將在第一條記錄之後退出。 – 2012-08-12 16:51:28

回答

0

更改的行

while(objCursor.isLast()); 

while(!objCursor.isLast()); 
+0

@eento - 沒有進一步的評論?我的答案是否正確?如果是這樣,我會感謝接受答案 – 2012-08-16 17:44:26

1

如果您要檢查表中是否有記錄。你可以使用這個。

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

它只會返回查找記錄。