How to: Create a custom DataAdapter

This how-to guide shows you how to create your own custom DataAdapter which only updates the chart when 10 points have been added to it.

In order to create a custom DataAdapter you need to do the following:

  1. Create a new class that extends DataAdapter
  2. Override the applicable methods
  3. Call notifyDataChanged() to tell the chart to update

shinobicharts for Android comes with the SimpleDataAdapter which tells the chart to update every time a point is added. This may not be appropriate if you are adding small amounts of data very frequently. With a custom data adapter you can control when the chart is updated.

First, create a new class that extends DataAdapter. You should keep it generic so you can reuse it.

Java

public class CustomDataAdapter<Tx, Ty> extends DataAdapter<Tx, Ty> {

}

C#

public class CustomDataAdapter : DataAdapter
{
}

Next override the add method so you can control when notifyDataChanged is called.

Java

    // We'll keep track of how many chart updates we have pending
    private int pendingUpdates = 0;

    @Override
    public boolean add(Data<Tx, Ty> dataPoint) {
        // Call the parent implementation
        boolean added = super.add(dataPoint);

        // Update the number of pending updates
        if (added) {
            pendingUpdates++;
        }

        if (pendingUpdates >= 10) {
            // If we have 10 updates pending, we fire the update handler.
            // Note that until this method is called, the chart will NOT be updated!
            notifyDataChanged();
            pendingUpdates = 0;
        }

        return added;
    }

C#

    // We'll keep track of how many chart updates we have pending
    private int pendingUpdates = 0;

    public override bool Add(IData dataPoint) {
        // Call the parent implementation
        bool added = base.Add(dataPoint);

        // Update the number of pending updates
        if (added) {
            pendingUpdates++;
        }

        if (pendingUpdates >= 10) {
            // If we have 10 updates pending, we fire the update handler.
            // Note that until this method is called, the chart will NOT be
            // updated!
            NotifyDataChanged();
            pendingUpdates = 0;
        }

        return added;
    }

Here the parent implementation of the add method is called first. The base DataAdapter handles the storage of your data points allowing you to define how it notifies the chart of any changes in your custom subclass. Of course, you are free to provide your own backing data store if you so wish.

Keeping track of the outcome of the add operation (with the boolean added variable) lets you only count successful additions. So, only increment the pendingUpdates variable if a point has been successfully added.

Finally, check to see how many pending updates there are. If 10 data points have been added you need to notify the chart to update itself. This is done by calling notifyDataChanged().

And that’s it! You can now use your CustomDataAdapter in your application. To see it in action take a look at the CustomDataAdapter sample project. This makes use of a subclassed ChartFragment to add data points every 200ms and the CustomDataAdapter to only update the chart every 10 points added. Of course, you may want to extend your own custom DataAdapter so that it also notifies the chart when its other methods are called, such as remove and clear.

See related code sample: Custom DataAdapter Sample, in the samples/custom-data-adapter folder of your product download (Xamarin.Android/samples/CustomDataAdapter if you’re using Xamarin.Android) .