【问题标题】:Sometimes SimpleCursorAdapter column '_id' does not exist, not always. Why?有时 SimpleCursorAdapter 列 '_id' 不存在,并非总是如此。为什么?
【发布时间】:2016-03-27 11:38:20
【问题描述】:

我使用 SimpleCursorAdapter 向 ListView 显示信息。

adapter = new SimpleCursorAdapter(
                this,
                R.layout.item_music,
                myCursor,
                new String[]{
                        MediaStore.Audio.Media.ARTIST,
                        MediaStore.Audio.Media.ALBUM,
                        MediaStore.Audio.Media.TITLE,
                        MediaStore.Audio.Media._ID},
                // To widget ids in the row layout...
                new int[]{
                        R.id.item_artist,
                        R.id.item_album,
                        R.id.item_title,
                        R.id.item_action});

它有时会工作,有时会因错误“列'_id'不存在”而崩溃。发生了什么?

这是我的 Logcat:

java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.support.v4.widget.CursorAdapter.swapCursor(CursorAdapter.java:344)
at android.support.v4.widget.SimpleCursorAdapter.swapCursor(SimpleCursorAdapter.java:326)

感谢您的帮助。

【问题讨论】:

    标签: android simplecursoradapter


    【解决方案1】:

    当您想从database 中选择某些内容并显示到listView 时,您需要拥有select _id

    例如:

    Cursor cur =  db.rawQuery( "select _id,* from your_table", null);
    

    【讨论】:

    • 我已经使用 CursorLoader 来获取光标,那么如何使用 select _id 作为您的建议?谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    相关资源
    最近更新 更多