我只是想拉一切从我的数据库类别:无法从CursorWindow读取第1行,第1列?
private static final int DB_VERSION = 1;
private static final String DB_NAME = "WishListDB.DB";
private static final String TABLE_ITEMS = "items_table";
private static final String COLUMN_INDEX = "item_index";
private static final String COLUMN_TITLE = "item_title";
private static final String COLUMN_CATEGORY = "item_category";
private static final String COLUMN_VENDOR = "item_vendor";
private static final String COLUMN_PRICE = "item_price";
private static final String COLUMN_IMAGE = "item_image";
所以我的总体目标是让所有的类别,并将其存储在一个ArrayList太然后传递到我的活动微调。这里就是我所说的数据:
db_manager = new databaseManager(this, null, null, 1);
int index_value = (int) db_manager.Get_Length_of_DB();
for (int i = 0; i <= index_value; i++)
{
category_List = db_manager.get_all_Categories();
}
ArrayAdapter categories_adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, category_List);
cat_spinner.setAdapter(categories_adapter);
这里是我的“get_all_categories”方法
public ArrayList<String> get_all_Categories()
{
String get_Cats_Query = "SELECT item_category from " + TABLE_ITEMS;
Cursor cursor = getReadableDatabase().rawQuery(get_Cats_Query, null);
cursor.moveToFirst();
ArrayList<String> cats_from_db = new ArrayList<String>() {};
if(!cursor.isAfterLast())
{
for (int i = 0; i < cursor.getCount(); i++)
{
cats_from_db.add(cursor.getString(i));
cursor.moveToNext();
}
}
cursor.close();
return cats_from_db;
}
和这里的错误:
Failed to read row 1, column 1 from a CursorWindow which has 2 rows, 1 columns.
java.lang.IllegalStateException: Couldn't read row 1, col 1 from
CursorWindow. Make sure the Cursor is initialized correctly before
accessing data from it.
我不是真的那么伟大与数据库,所以我真的很苦恼,我已经成功地添加到数据库,但无法弄清楚如何获取数据。如果有人可以帮助它将不胜感激,谢谢! :)
明显'Cursor.getString(INT)'需要字段(列)数量没有行号作为参数 – Selvin