我有一个sqliteDatabase帮助器,它仅从数据库返回一个带有名称列的游标:
public Cursor getnames() { Cursor cursor = db.query(table_name, new String[] {name}, null, null, null, null, null); return cursor;}
我试图将此光标绑定到布局中包含的一个简单的ListVIEw:
<ListVIEw androID:ID="@androID:ID/List" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" ></ListVIEw>
Java:
Cursor cursor = db.getnames(); startManagingCursor(cursor); listadapter adapter = new SimpleCursorAdapter ( this, androID.R.layout.simple_List_item_1, cursor, new String[] {constants.name}, new int[] {androID.R.ID.text1} ); setlistadapter(adapter);
我已按照我可以找到的所有教程进行 *** 作,但该应用程序仍然意外停止.请告诉我我做错了!
这是LOGCAT跟踪,希望这就是您需要的:
12-03 17:18:41.557: E/AndroIDRuntime(30413): FATAL EXCEPTION: main12-03 17:18:41.557: E/AndroIDRuntime(30413): java.lang.RuntimeException: Unable to start activity ComponentInfo{george.frost.YourCarbDatabase/com.androID.CarbCount.Search}: java.lang.IllegalArgumentException: column '_ID' does not exist12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1659)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.app.ActivityThread.access00(ActivityThread.java:121)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:943)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.os.Handler.dispatchMessage(Handler.java:99)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.os.Looper.loop(Looper.java:130)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.app.ActivityThread.main(ActivityThread.java:3701)12-03 17:18:41.557: E/AndroIDRuntime(30413): at java.lang.reflect.Method.invokeNative(Native Method)12-03 17:18:41.557: E/AndroIDRuntime(30413): at java.lang.reflect.Method.invoke(Method.java:507)12-03 17:18:41.557: E/AndroIDRuntime(30413): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)12-03 17:18:41.557: E/AndroIDRuntime(30413): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:624)12-03 17:18:41.557: E/AndroIDRuntime(30413): at dalvik.system.NativeStart.main(Native Method)12-03 17:18:41.557: E/AndroIDRuntime(30413): Caused by: java.lang.IllegalArgumentException: column '_ID' does not exist12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.Widget.CursorAdapter.init(CursorAdapter.java:111)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.Widget.CursorAdapter.<init>(CursorAdapter.java:90)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.Widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.Widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)12-03 17:18:41.557: E/AndroIDRuntime(30413): at com.androID.CarbCount.Search.onCreate(Search.java:42)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)12-03 17:18:41.557: E/AndroIDRuntime(30413): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1623)12-03 17:18:41.557: E/AndroIDRuntime(30413): ... 11 more
解决方法:
不推荐使用startManagingCursor()方法.您应该改用Loader API.如果您要定位的API级别低于11的设备,则需要使用compatibility library.
由于它表明您只是想从sqliteDatabase加载某些数据,因此您无需创建ContentProvIDer,但可以扩展Loader.可以在此处找到示例和更多信息:CursorLoader usage without ContentProvider
另外,如果“活动”仅显示ListVIEw,则可能要切换到ListActivity
,因为它使绑定数据和获取ID(例如)更加容易.
自您发布LogCat以来:
问题在于,SimpleCursorAdapter类需要一个名为_ID的列来获取数据库中每一行数据的ID.有关该问题的更多信息,请参见:Android column ‘_id’ does not exist?
总结以上是内存溢出为你收集整理的Java-Android,将ListView绑定到SQLite数据库全部内容,希望文章能够帮你解决Java-Android,将ListView绑定到SQLite数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)