Android Open Source - arcgis-runtime-samples-android Feature Layer Utils






From Project

Back to project page arcgis-runtime-samples-android.

License

The source code is released under:

Apache License

If you think the Android project arcgis-runtime-samples-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Copyright 2014 ESRI
 *//from w  ww.  j a v  a 2  s .  co m
 * All rights reserved under the copyright laws of the United States
 * and applicable international laws, treaties, and conventions.
 *
 * You may freely redistribute and use this sample code, with or
 * without modification, provided you include the original copyright
 * notice and use restrictions.
 *
 * See the sample code usage restrictions document for further information.
 *
 */

package com.esri.arcgis.android.samples.attributeeditor;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import com.esri.core.map.FeatureType;
import com.esri.core.map.Field;
import com.esri.core.map.Graphic;

public class FeatureLayerUtils {

  public enum FieldType {
    NUMBER, STRING, DECIMAL, DATE;

    public static FieldType determineFieldType(Field field) {

      if (field.getFieldType() == Field.esriFieldTypeString) {
        return FieldType.STRING;
      } else if (field.getFieldType() == Field.esriFieldTypeSmallInteger
          || field.getFieldType() == Field.esriFieldTypeInteger) {
        return FieldType.NUMBER;
      } else if (field.getFieldType() == Field.esriFieldTypeSingle
          || field.getFieldType() == Field.esriFieldTypeDouble) {
        return FieldType.DECIMAL;
      } else if (field.getFieldType() == Field.esriFieldTypeDate) {
        return FieldType.DATE;
      }
      return null;
    }
  }

  /**
   * Helper method to determine if a field should be shown in the list for
   * editing
   */
  public static boolean isFieldValidForEditing(Field field) {

    int fieldType = field.getFieldType();

    if (field.isEditable() && fieldType != Field.esriFieldTypeOID
        && fieldType != Field.esriFieldTypeGeometry
        && fieldType != Field.esriFieldTypeBlob
        && fieldType != Field.esriFieldTypeRaster
        && fieldType != Field.esriFieldTypeGUID
        && fieldType != Field.esriFieldTypeXML) {

      return true;

    }

    return false;
  }

  /**
   * Helper method to set attributes on a graphic. Only sets the attributes on
   * the newGraphic variable if the value has changed. Returns true if the
   * value has changed and has been set on the graphic.
   * 
   * @return boolean hasValueChanged
   */
  public static boolean setAttribute(Map<String, Object> attrs,
      Graphic oldGraphic, Field field, String value, DateFormat formatter) {

    boolean hasValueChanged = false;

    // if its a string, and it has changed from the oldGraphic value
    if (FieldType.determineFieldType(field) == FieldType.STRING) {

      if (!value.equals(oldGraphic.getAttributeValue(field.getName()))) {

        // set the value as it is
        attrs.put(field.getName(), value);
        hasValueChanged = true;

      }
    } else if (FieldType.determineFieldType(field) == FieldType.NUMBER) {

      // if its an empty string, its a 0 number value (nulls not
      // supported), check this is a
      // change before making it a 0
      if (value.equals("")
          && oldGraphic.getAttributeValue(field.getName()) != Integer
              .valueOf(0)) {

        // set a null value on the new graphic
        attrs.put(field.getName(), 0);
        hasValueChanged = true;

      } else {

        // parse as an int and check this is a change
        int intValue = Integer.parseInt(value);
        if (intValue != Integer.parseInt(oldGraphic.getAttributeValue(
            field.getName()).toString())) {

          attrs.put(field.getName(), Integer.valueOf(intValue));
          hasValueChanged = true;

        }
      }
    } else if (FieldType.determineFieldType(field) == FieldType.DECIMAL) {

      // if its an empty string, its a 0 double value (nulls not
      // supported), check this is a
      // change before making it a 0
      if ((value.equals("") && oldGraphic.getAttributeValue(field
          .getName()) != Double.valueOf(0))) {

        // set a null value on the new graphic
        attrs.put(field.getName(), 0.0);
        hasValueChanged = true;

      } else {

        // parse as an double and check this is a change
        double dValue = Double.parseDouble(value);
        if (dValue != Double.parseDouble(oldGraphic.getAttributeValue(
            field.getName()).toString())) {

          attrs.put(field.getName(), Double.valueOf(dValue));
          hasValueChanged = true;

        }
      }
    } else if (FieldType.determineFieldType(field) == FieldType.DATE) {

      // if its a date, get the milliseconds value
      Calendar c = Calendar.getInstance();
      long dateInMillis = 0;

      try {

        // parse to a double and check this is a change
        c.setTime(formatter.parse(value));
        dateInMillis = c.getTimeInMillis();

        if (dateInMillis != Long.parseLong(oldGraphic
            .getAttributeValue(field.getName()).toString())) {

          attrs.put(field.getName(), Long.valueOf(dateInMillis));
          hasValueChanged = true;
        }
      } catch (ParseException e) {
        // do nothing
      }
    }
    // }

    return hasValueChanged;

  }

  /**
   * Helper method to find the Types actual value from its name
   */
  public static String returnTypeIdFromTypeName(FeatureType[] types,
      String name) {

    for (FeatureType type : types) {
      if (type.getName().equals(name)) {
        return type.getId();
      }
    }
    return null;
  }

  /**
   * Helper method to setup the editable field indexes and store them for
   * later retrieval in getItem() method.
   */
  public static int[] createArrayOfFieldIndexes(Field[] fields) {

    // process count of fields and which are available for editing
    ArrayList<Integer> list = new ArrayList<Integer>();
    int fieldCount = 0;

    for (int i = 0; i < fields.length; i++) {

      if (isFieldValidForEditing(fields[i])) {

        list.add(Integer.valueOf(i));
        fieldCount++;

      }
    }

    int[] editableFieldIndexes = new int[fieldCount];

    for (int x = 0; x < list.size(); x++) {

      editableFieldIndexes[x] = list.get(x).intValue();

    }

    return editableFieldIndexes;
  }

  /**
   * Helper method to create a String array of Type values for populating a
   * spinner
   */
  public static String[] createTypeNameArray(FeatureType[] types) {

    String[] typeNames = new String[types.length];
    int i = 0;
    for (FeatureType type : types) {

      typeNames[i] = type.getName();
      i++;

    }

    return typeNames;

  }

  /**
   * Helper method to create a HashMap of types using the Id (value) as the
   * key
   */
  public static HashMap<String, FeatureType> createTypeMapByValue(
      FeatureType[] types) {

    HashMap<String, FeatureType> typeMap = new HashMap<String, FeatureType>();

    for (FeatureType type : types) {

      typeMap.put(type.getId(), type);

    }

    return typeMap;

  }

}




Java Source Code List

com.arcgis.android.samples.ExportTileCacheTask.ExportTileCacheTaskSampleActivity.java
com.arcgis.android.samples.cloudportal.featureservicetablequery.MainActivity.java
com.arcgis.android.samples.cloudportal.querycloudfeatureservice.MainActivity.java
com.arcgis.android.samples.dynamiclayer.DynamicLayerRendererActivity.java
com.arcgis.android.samples.geometrysample.BufferFragment.java
com.arcgis.android.samples.geometrysample.GeometrySampleActivity.java
com.arcgis.android.samples.geometrysample.GeometryUtil.java
com.arcgis.android.samples.geometrysample.SampleListFragment.java
com.arcgis.android.samples.geometrysample.SpatialRelationshipsFragment.java
com.arcgis.android.samples.geometrysample.UnionDifferenceFragment.java
com.arcgis.android.samples.localdata.localrasterdata.EditTextUtils.java
com.arcgis.android.samples.localdata.localrasterdata.FileBrowserFragment.java
com.arcgis.android.samples.localdata.localrasterdata.HillshadeRendererParametersFragment.java
com.arcgis.android.samples.localdata.localrasterdata.MainActivity.java
com.arcgis.android.samples.localdata.localrasterdata.OnDialogDismissListener.java
com.arcgis.android.samples.localdata.localrasterdata.RendererType.java
com.arcgis.android.samples.localdata.localrasterdata.StretchParametersFragment.java
com.arcgis.android.samples.maps.basemaps.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.BasemapListFragment.java
com.arcgis.android.samples.maps.fragmentmanagement.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.MapFragment.java
com.arcgis.android.samples.maps.helloworld.MainActivity.java
com.arcgis.android.samples.maps.maplegend.LegendDialogFragment.java
com.arcgis.android.samples.maps.maplegend.MainActivity.java
com.arcgis.android.samples.maps.maprotation.Compass.java
com.arcgis.android.samples.maps.maprotation.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MapFragment.java
com.arcgis.android.samples.milsym2525c.MainActivity.java
com.arcgis.android.samples.milsym2525c.Mil2525cMessageParser.java
com.arcgis.android.samples.oauth2sample.MapFragment.java
com.arcgis.android.samples.oauth2sample.OAuth2Sample.java
com.arcgis.android.samples.oauth2sample.UserContentActivity.java
com.arcgis.android.samples.oauth2sample.UserContentArrayAdapter.java
com.arcgis.android.samples.oauth2sample.UserContentFragment.java
com.arcgis.android.samples.oauth2sample.UserWebmaps.java
com.arcgis.android.samples.search.placesearch.MainActivity.java
com.esri.android.sample.closestfacilities.MainActivity.java
com.esri.android.samples.mbtiles.LocalMBTiles.java
com.esri.android.samples.mbtiles.MBTilesLayer.java
com.esri.android.samples.mgrsgrid.LocateMGRSActivity.java
com.esri.arcgis.android.sample.runtimegeodb.CreateRuntimeGeodatabaseActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapFragment.java
com.esri.arcgis.android.sample.simplemap.SingleFragmentActivity.java
com.esri.arcgis.android.samples.GeoJSONEarthquakeMap.GeoJSONEarthquakeMapActivity.java
com.esri.arcgis.android.samples.PopupUICustomization.LayerQueryTask.java
com.esri.arcgis.android.samples.PopupUICustomization.MyAttachmentsView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyEditAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyReadOnlyAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyTitleView.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupFragment.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupUICustomizationActivity.java
com.esri.arcgis.android.samples.addcsv2graphic.AddCSVActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeEditorActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeItem.java
com.esri.arcgis.android.samples.attributeeditor.AttributeListAdapter.java
com.esri.arcgis.android.samples.attributeeditor.FeatureLayerUtils.java
com.esri.arcgis.android.samples.basiclicense.MainActivity.java
com.esri.arcgis.android.samples.basiclicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.classbreaksrenderer.ClassBreaksRendererActivity.java
com.esri.arcgis.android.samples.featuredusergroup.FeaturedGroupsActivity.java
com.esri.arcgis.android.samples.featuredusergroup.GroupsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.ItemsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.MapActivity.java
com.esri.arcgis.android.samples.geometryeditor.ConfirmDiscardDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.EditFailedDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeData.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.GeometryEditorActivity.java
com.esri.arcgis.android.samples.identifytask.Identify.java
com.esri.arcgis.android.samples.measure.MainActivity.java
com.esri.arcgis.android.samples.nearby.Nearby.java
com.esri.arcgis.android.samples.offlineeditor.GDBUtil.java
com.esri.arcgis.android.samples.offlineeditor.OfflineActions.java
com.esri.arcgis.android.samples.offlineeditor.OfflineEditorActivity.java
com.esri.arcgis.android.samples.offlineeditor.TemplatePicker.java
com.esri.arcgis.android.samples.offlineroutingandgeocoding.RoutingAndGeocoding.java
com.esri.arcgis.android.samples.popupinwebmapforediting.PopupInWebmapForEditing.java
com.esri.arcgis.android.samples.popupinwebmapforviewing.PopupInWebmapForViewing.java
com.esri.arcgis.android.samples.querytask.MainActivity.java
com.esri.arcgis.android.samples.routing.MyAdapter.java
com.esri.arcgis.android.samples.routing.RoutingDialogFragment.java
com.esri.arcgis.android.samples.routing.RoutingListFragment.java
com.esri.arcgis.android.samples.routing.RoutingSample.java
com.esri.arcgis.android.samples.servicearea.EditFragment.java
com.esri.arcgis.android.samples.servicearea.ServiceAreaSample.java
com.esri.arcgis.android.samples.standardlicense.MainActivity.java
com.esri.arcgis.android.samples.standardlicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.standardlicenseoffline.MainActivity.java
com.esri.arcgis.android.samples.standardlicenseoffline.MessageDialogFragment.java
com.esri.arcgis.android.samples.uniquevaluerenderer.UniqueValueRendererSampleActivity.java
com.esri.arcgis.android.samples.viewshed.Viewshed.java
com.esri.arcgis.android.samples.wmslayer.MainActivity.java
com.esri.arcgis.samples.offlineanalysis.MainActivity.java