Listview with CursorAdapter

半腔热情 提交于 2019-11-30 02:25:43
Sam

I noticed a few points:

  1. A CursorAdapter moves the Cursor for you, take out your call to cursor.moveToNext().
  2. The adapter's getView() calls newView() and bindView() on it's own; you shouldn't call these methods yourself.
  3. You should watch the Android developer's lectures at Google IO to learn tips and tricks about speeding up your adapter. Tips like:
    • Using a ViewHolder, rather than calling findViewById() repeatedly.
    • Saving the indices of your Cursor, rather than calling getColumnIndex() repeatedly.
    • Fetching the LayoutInflater once and keeping a local reference.

Also, I suggest you switch from using CursorManager to using CursorLoader. This is documented in the Android API guide, under Loaders. A specific example that you might find useful is here.

A Cursor Adapter "connects" a Cursor to a ListView. The Cursor is a data view of the data, and the ListView is a UI view of the same data. You don't need to program anything to make the ListView stay in sync with the Cursor, that's all handled automatically.

You do need to tell the ListView which columns in the Cursor it should display, See the documentation for the SimpleCursorAdapter class. I usually use that class unless I have to modify the data as I move it from the Cursor to the ListView.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!