2 o casper ne O_Casper_ne 于 2013.05.28 09:46 提问

如何从数据库中获取随机字符串 ?

我创建的一个程序,第一个 activity 从数据库中获取随机值然后作为一个字符串传递到下一个activity 中。
我知道在 sql 中如何实现这个功能:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

一些尝试后我在程序中使用:

Cursor send = database.query(TABLE_NAME, new String[] { COLUMN_NAME }, null, null, null, null, "RANDOM() LIMIT 1");

Bundle b = new Bundle();
b.putString("description", send.getString(send.getColumnIndex(COLUMN_NAME)));

当我点击按钮打开第二个 activity 时,最后一行中有错误。
Log:

05-22 22:14:24.202: E/AndroidRuntime(14259): FATAL EXCEPTION: main
05-22 22:14:24.202: E/AndroidRuntime(14259): android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
05-22 22:14:24.202: E/AndroidRuntime(14259): at com.example.ListAction$1.onItemClick(ListAction.java:90)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.widget.AbsListView.performItemClick(AbsListView.java:1177)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2705)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.widget.AbsListView$1.run(AbsListView.java:3458)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.os.Handler.handleCallback(Handler.java:605)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.os.Looper.loop(Looper.java:137)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.app.ActivityThread.main(ActivityThread.java:4507)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at java.lang.reflect.Method.invoke(Method.java:511)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at dalvik.system.NativeStart.main(Native Method)

如何获取复选框偏好价值?

1个回答

wurenbi45
wurenbi45   2013.05.28 17:24
已采纳

数组越界了,可能是query用法有问题,导致没有数据查询出来,sqlite跟sql的用法可能有出入.

Csdn user default icon
上传中...
上传图片
插入图片