我正在检查,以便将我最喜欢的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;
}
使用1或2个条目为我的promoFavoriteArray工作正常,但是我把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 ..
答案 0 :(得分:0)
你正在做(伪代码):
first
add(current)
next
repeat if last
如果有2个项目,在您添加之后,接下来使其成为索引2,则循环并再次添加。你有两件事,很棒。
如果您添加一次的3个项目,它不会持续,所以它不会再次循环。你最终得到了一个。
相反,尝试类似的事情(你不需要isFirst检查,而你需要不最后):
if (objCursor == null)
return array; // empty
objCursor.moveToFirst();
do
{
// read data
array.Add(promo);
} while (!objCursor.isLast())
答案 1 :(得分: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;