【发布时间】:2015-08-17 12:18:28
【问题描述】:
我试图从一个从数据库中获取信息的微调器中获取一个字符串。使用时
spinner.getSelectedItem().toString();
我得到像“android.database.sqlite.SQLiteCursor@412f3ff8”这样的字符串。为了获得解析的字符串,我尝试使用游标查询数据库,但它会导致 IllegalStatException,同时 LogCat 中出现一条消息说“无法从具有 3 行 2 列的 CursorWindow 读取第 1 行第 2 列。”。
行和列的数字是正确的,因为应该在那里找到的单元格包含我要查询的值。
这是我使用的代码:
int selection = (int) spinner.getSelectedItemId(); //I get the selected item id.
Cursor selectioncursor = db.getAllSubjects(); //This is a query getting all the contents from the table
selectioncursor.moveToPosition(selection); //The id I got earlier equals the number of the row, the data is stored in, so I move the cursor to the correct row.
String subject = selectioncursor.getString(2); //Now the cursor should get the string from column 2 which is the one containing all the values (first column is "_id" of course)
谢谢你的帮助。
【问题讨论】:
-
我可以建议你一件事。如果你想要第 2 列中的值,你需要查询 selectioncursor.getString(1);而不是 selectioncursor.getString(2);因为光标索引从 0 开始。请您发布 logcat 吗
-
@SharathG 这确实解决了我的问题。谢谢你:)
-
哦,很高兴听到它已经自己解决了。欢呼
标签: android database cursor illegalstateexception