从数据库获取数据用于Android图
我已经在我的 Android 应用程序中设置了 SQLite 数据库。我使用了谷歌的教程,我的数据库可以工作(我可以将数据放入其中并以其他形式读出)。现在,我想获取列价格并将其放入图表中。我已经可以用硬编码值绘制图表了。我正在尝试:
Cursor item0 = _dbHelper.fetcAllBoughtItems();
startManagingCursor(item0);
String xaxis[]=new String[4];
xaxis[0]= item0.getString(item0.getColumnIndexOrThrow(databaseManager.KEY_BOUGHT_ITEM));
xaxis[1]="2007";
xaxis[2]="2008";
xaxis[3]="2009";
我收到以下错误(我知道它告诉我出了什么问题,但我无法修复它)
04-27 10:03:21.382: ERROR/AndroidRuntime(6920): java.lang.RuntimeException: Unable to start activity ComponentInfo{Adforce.files/Adforce.files.summarySpendBudget}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5
有人可以帮助我吗? 问候
编辑: fetchAllBoughtItems() 代码是: public Cursor fetchAllBoughtItems(){ //获取所有已购买商品的函数 返回 mDb.query(DATABASE_TABLE_ItemB, new String[]{KEY_ROWID_ItemB, KEY_BOUGHT_ITEM, KEY_PRICE_ItemB }, null, null, null, null, null); }
I have setup a SQLite database in my android application. I used a tutorial from google and my database works (I can put data in it and read it out in an other form). Now, I want to get the column price and put it in a graph. I can draw the graph already with hard coded values. I'm trying:
Cursor item0 = _dbHelper.fetcAllBoughtItems();
startManagingCursor(item0);
String xaxis[]=new String[4];
xaxis[0]= item0.getString(item0.getColumnIndexOrThrow(databaseManager.KEY_BOUGHT_ITEM));
xaxis[1]="2007";
xaxis[2]="2008";
xaxis[3]="2009";
I get the follow error (I know it's telling me what's wrong, but I can't get it fixed)
04-27 10:03:21.382: ERROR/AndroidRuntime(6920): java.lang.RuntimeException: Unable to start activity ComponentInfo{Adforce.files/Adforce.files.summarySpendBudget}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5
Can someone help me please?
Regards
Edit:
fetchAllBoughtItems() code is:public Cursor fetchAllBoughtItems(){ //function to fetch all bought items
return mDb.query(DATABASE_TABLE_ItemB, new String[]{KEY_ROWID_ItemB, KEY_BOUGHT_ITEM, KEY_PRICE_ItemB }, null, null, null, null, null);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fetcAllBoughtItems() 返回没有名为databaseManager.KEY_BOUGHT_ITEM 列的游标
修复 fetcAllBoughtItems()
编辑:
在
item0.getString(...
之前添加item0.moveToFirst()
code> 并检查是否返回 truefetcAllBoughtItems() returns Cursor without column named databaseManager.KEY_BOUGHT_ITEM
fix fetcAllBoughtItems()
EDIT:
add
item0.moveToFirst()
beforeitem0.getString(...
and check if will return true