我正在检查以将我最喜欢的自己的 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 ..