Skip to content

Cross platform data source

Sami M. Kallio edited this page Oct 9, 2013 · 1 revision

This class was born from the need to display collections quickly without having to write data source implementations every single time. In its simplest use it will bind an ObservableCollection into iOS UITableView and UICollectionView controls and on Android to ListView and Spinner controls. The default cell is going to be similar to what one would expect from a C# list view control; a text displaying the objects ToString() output. To enable custom views one would need to create a custom class inheriting from the before mentioned controls, implementing a custom interface ITableCellProvider. Datasource will serve multiple consumers.

// create a new datasource from existing collection (optional)
var dataSource = new ObservableDataSource<EditableText> (<insert enumerable list from f.e. SQLite database>);
// bind a consumer, f.e. UITableView
datasource.Bind(<data consumer>);
// add new item, will automatically refresh the consumer
dataSource.Add(new EditableText());

Example Android implementation returning a custom view:

public class EditableTextTable : ListView, ITableCellProvider<EditableText>
{
	public EditableTextTable(Context context) : base(context)
	{
	}

	public EditableTextTable(Context context, IAttributeSet attrs) :  base(context, attrs) 
	{

	}

	public EditableTextTable(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) 
	{

	}

	#region ITableCellProvider implementation
	public View GetView (EditableText item, View convertView)
	{
		var editableCell = convertView as EditableTextCell ?? new EditableTextCell(this.Context);

		editableCell.Bind (item);

		return editableCell;
	}
	#endregion
}

For more details run ObservableCollectionTest applications from [iOS|Android]\Tests\ObservableCollectionTest or alternative StockQuote.

Clone this wiki locally