Editing using the API

Editing using the API

Edits take place on the feature layer itself. Take a look at the applyEdits method on the ArcGISFeatureLayer class

public void applyEdits(Graphic[] adds,
                       Graphic[] deletes,
                       Graphic[] updates,
                       CallbackListener<FeatureEditResult[][]> callback)

Notice here that you pass an array of Graphic objects to either be added, deleted, or updated. These edits are only applicable on feature layers.

For new features that get added, the applyEdits will submit all the features back to the geodatabase behind the service, either from the local server or from online. If successful, all of the new features will be updated with the ObjectID (OID) and GID and will be added to the layer. For updates and deletes, the graphic features will be adjusted in the displayed layer based on a successful response. The response is handled within a CallbackListener.

Consider the following code example for adding a single point feature to a layer:

Point mapPt = map.toMapPoint(e.getX(), e.getY());
Map<String,Object> attributes = new HashMap<String,Object>();
attributes.put("Type", "Park");
attributes.put("Description", "Editing...");
Graphic g = new Graphic(mapPt, new SimpleMarkerSymbol(Color.red, 10, SimpleMarkerSymbol.Style.CIRCLE), attributes,0);
Graphic[] adds = {g};

	 new Graphic[]{}, 
	 new Graphic[]{}, 
		new CallbackListener<FeatureEditResult[][]>(){

			public void onCallback(FeatureEditResult[][] objs) {
    // do something with the result object 'objs'

			public void onError(Throwable e) {
    // handle the error

What is happening in this case is the following:

  1. A new Graphic instance is created with a Point geometry and a collection of attributes
  2. The graphic is added to an array
  3. The applyEdits method is invoked, passing in the graphics to add to the feature layer
  4. An anonymous CallbackListener is defined, handling the successful callback or errors that result.