Listview with CursorAdapter

前端 未结 2 1518
天命终不由人
天命终不由人 2020-12-28 10:13

I\'m developing an application which displays Phone contacts with CursorAdapter. When I run it, I faced with a list view which repeated just one contact as bellow (\"david\

相关标签:
2条回答
  • 2020-12-28 10:51

    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.
    0 讨论(0)
  • 2020-12-28 11:11

    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.

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