Using a CursorAdapter

Sometimes our data comes from a source that returns an ICursor element; thus, we can make use of the SimpleCursorAdapter type without having to create a collection of data objects.

How to do it...

Using the SimpleCursorAdapter type with an ICursor instance is almost the same as using the SimpleAdapter type with an object collection:

  1. When using a CursorAdapter type, all we need is an ICursor instance from either the ContentResolver or CursorLoader types:
    var uri = ContactsContract.Contacts.ContentUri;
    string[] projection = {
      ContactsContract.Contacts.InterfaceConsts.Id,
      ContactsContract.Contacts.InterfaceConsts.DisplayName,
      ContactsContract.Contacts.InterfaceConsts
        .ContactStatusLabel,
    };
    ICursor cursor = ContentResolver.Query(
      uri, projection, null, null, null);
  2. Once we have the cursor, we can hand it over to the SimpleCursorAdapter type, which is very similar to the SimpleAdapter type:
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
      this, Android.Resource.Layout.SimpleListItem2, cursor,
      new[] {
        ContactsContract.Contacts.InterfaceConsts.DisplayName,
        ContactsContract.Contacts.InterfaceConsts
          .ContactStatusLabel
      }, new[] {
        Android.Resource.Id.Text1,
        Android.Resource.Id.Text2 
      });
  3. We can then assign this adapter to the list view as we would do for any other adapter:
    listView.Adapter = adapter

How it works...

When our data comes from a content provider, we are presented with an ICursor instance. In order to handle this type of result, we make use of the SimpleCursorAdapter type, which is very similar to the SimpleAdapter type.

The main difference is that, instead of accepting a collection of items as data, the SimpleCursorAdapter type accepts an ICursor instance. And, instead of using dictionary keys to map data to a view resource ID, we use the column names from the projection.

This is very useful if we are trying to read data from an ICursor instance, but we don't need to create an object collection just in order to bind to a list view. As content providers are used to share content between apps, we may have to consume one at some time.

There's more...

If we want to have greater flexibility for more complex data, we can do something similar to what we do when using a BaseAdapter<T> type. In order to have more control, we inherit from the CursorAdapter type, which works with an ICursor instance instead of a collection of data items.

The CursorAdapter type is different in that we don't have to implement the various data members, such as GetItem(), GetItemId(), Count, and the indexer. The reason for this is that the implementation of these members is handled by the CursorAdapter automatically; comes from the cursor; all we have to do is work with the view.

Again, unlike the BaseAdapter type, we don't implement the GetView() method but rather it is split into two more specific methods, NewView() and BindView(). In the GetView() method, we will inflate or create the view and then assign the data to the view. With the CursorAdapter type, we will inflate or create the view in the NewView() method and then only assign the data to the view in the BindView() method. The actual implementation is very similar, but it is split into two methods.

See also

  • The Implementing a ListView recipe
  • The Using a SimpleAdapter recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset