The Problem
- We have a Database holding all our data
- We can get a Cursor with the data we want to display
- We want to display the data in a ListView
A Possible Solution
- Create an ArrayList
- Iterate over the Cursor and add its contents to the ArrayList
- Use an ArrayAdapter to connect to the ListView
Issues with the Solution
Complication
- We need to create a temporary collection each time
- We need to iterate all the items in the Cursor
Issues with the Solution
Efficiency / Performance
- For a very big Cursor will take a lot of time iterate it
- Even if the user only looks at the first few items
Issues with the Solution
Limiting
- Hard to display a complex View for each item
A Better Solution
An Adapter for Cursors
Enter SimpleCursorAdapter
An Adapter that wraps a Cursor
SimpleCursorAdapter
Connects columns in the Cursor data to TextViews and ImageViews in a Layout
SimpleCursorAdapter
Syntax
// FROM
String[] cols = new String[] { NAME_COL_NAME, DESCRIPTION_COL_NAME };
// TO
int[] ids = new int[] { R.id.name_txt, R.id.desc_txt};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, // Context
R.layout.item, // Item layout
cursor, // Results cursor
cols, // Columns
ids, // Views
0); // Special flags
But What If We Need More?
- Column data that requires processing before display
- Data that does not fit neither a TextView nor ImageView
Meet the ViewBinder
- Allow basic columns → id mapping
- Enable manual override for some mappings
ViewBinder
- Allows customizing the translation of Cursor data by handling specific
columns' data presentations
Thanks!
Just thought of a new question?
courses@amitkotlovski.com