Delete features (feature service)

View inJavaKotlin
View on GitHub
Sample viewer app

Delete features from an online feature service.

Image of delete features feature service

Use case

Sometimes users may want to delete features from an online feature service.

How to use the sample

Tap on a feature on the map and tap the trash icon to delete it.

How it works

  1. Create a ServiceFeatureTable object from a URL.
  2. Create a FeatureLayer object from the ServiceFeatureTable.
  3. Select features from the FeatureTable via queryFeaturesAsync().
  4. Remove the selected features from the ServiceFeatureTable using deleteFeaturesAsync().
  5. Update the table on the server using applyEditsAsync().

Relevant API

  • Feature
  • FeatureLayer
  • ServiceFeatureTable

Tags

deletion, feature, online, Service, table

Sample Code

ConfirmDeleteFeatureDialog.javaMainActivity.java
                                                                    
/*
 * Copyright 2019 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.esri.arcgisruntime.samples.deletefeaturesfeatureservice;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.appcompat.app.AlertDialog;

public class ConfirmDeleteFeatureDialog extends DialogFragment {

  private static final String ARG_FEATURE_ID = ConfirmDeleteFeatureDialog.class.getSimpleName() + "_feature_id";

  private String featureId;

  private DialogInterface.OnClickListener mOnClickListener = (dialog, which) -> {
    if (getContext() instanceof OnButtonClickedListener) {
      if (which == DialogInterface.BUTTON_POSITIVE) {
        ((OnButtonClickedListener) getContext()).onDeleteFeatureClicked(featureId);
      } else {
        dismiss();
      }
    }
  };

  public static ConfirmDeleteFeatureDialog newInstance(String featureId) {
    ConfirmDeleteFeatureDialog fragment = new ConfirmDeleteFeatureDialog();
    Bundle args = new Bundle();
    args.putString(ARG_FEATURE_ID, featureId);
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.featureId = getArguments().getString(ARG_FEATURE_ID);
  }

  @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    return new AlertDialog.Builder(getContext())
        .setMessage(getString(R.string.dialog_confirm_delete_message, featureId))
        .setPositiveButton(R.string.dialog_confirm_delete_positive, mOnClickListener)
        .setNegativeButton(R.string.dialog_confirm_delete_negative, mOnClickListener)
        .create();
  }

  public interface OnButtonClickedListener {
    void onDeleteFeatureClicked(String featureId);
  }

}