Android ListView Database Exception

前端 未结 1 587
梦毁少年i
梦毁少年i 2021-01-26 16:27

I have been a naughty boy and I\'ve copied a method from official Notepad application from android developer site, this is my class :

package com.example.prva;

         


        
相关标签:
1条回答
  • 2021-01-26 17:17

    See the documentation for CursorAdapter:

    The Cursor must include a column named _id or this class will not work.

    In your code, you use the SimpleCursorAdapter, which is a derived class, so it appears this statement applies.

    Cursors are like iterators or pointers, they contain nothing but a mechanism for transversing the data, they contain no columns themselves.

    From another documentation, you can understand the statement above better:

    Handling content URI IDs

    By convention, providers offer access to a single row in a table by accepting a content URI with an ID value for the row at the end of the URI. Also by convention, providers match the ID value to the table's _ID column, and perform the requested access against the row that matches.

    This convention facilitates a common design pattern for apps accessing a provider. The app does a query against the provider and displays the resulting Cursor in a ListView using a CursorAdapter. The definition of CursorAdapter requires one of the columns in the Cursor to be _ID.

    Several ways for you to fix the problem:

    1. Add a column "_id" in your table.

    2. Using alias to get the curosr. For example:

      SELECT someid as _id, name, number FROM TABLE1;

    0 讨论(0)
提交回复
热议问题