Getting to Know Simple CursorAdapter

The SimpleCursorAdapter, which we've used many times already, is depicted in Figure 4-21.



Figure 4-21. The SimpleCursorAdapter

The constructor of SimpleCursorAdapter looks like this: SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to). This adapter converts a row in the cursor to a child view for the container control. The definition of the child view is defined in an XML resource (layout parameter). Note that because a row in the cursor might have many columns, you tell the SimpleCursorAdapter which columns you want to select from the row by specifying an array of column names (using the from parameter).

Similarly, because each column you select is mapped to a TextView, you must specify the IDs in the to parameter. There's a one-to-one mapping between the column that you select and a TextView that displays the data in the column, so the from and to parameters must be the same size.

Figure 4-21 reveals some flexibility in using adapters. Because the container control operates on an adapter, you can substitute various types of adapters based on your data and child view. For example, if you are not going to populate an AdapterView from the database, you don't have to use the SimpleCursorAdapter. You can opt for an even "simpler" adapter—the ArrayAdapter.

0 0

Post a comment