Using ArcGIS android API,
I need to count the number of features in a specfic layer for use later in my app.
APPROACH:
After my map's getLoadStatus() returns "LOADED", I iterate through my map's layers. When the appropriate layer is found I cast the layer to a FeatureLayer, then get the feature table and count of items in that table. Code below:
// Note 'map' is an ArcGISMap object MainActivity.this.arcGISMap = map; // save ArcGISMap reference for later MainActivity.this.mMapView.setMap(map); LayerList mMapOperationalLayers = MainActivity.this.arcGISMap.getOperationalLayers(); // get map's LayerList for (final Layer layer : mMapOperationalLayers) { Log.d("layer", layer.getName()); if ((layer.getName().toUpperCase().equals("LAYER_OF_INTEREST")) { MainActivity.this.layer_of_interest = (FeatureLayer) layer; // save layer reference for later //MainActivity.this.pages_count = (((FeatureLayer) layer).getFeatureTable()).getTotalFeatureCount(); MainActivity.this.pages_count = MainActivity.this.layer_of_interest.getFeatureTable().getTotalFeatureCount(); Log.d("loadMobileMapPackage", "pages_count:" + MainActivity.this.pages_count); } // end if } // end for
PROBLEM
'getTotalFeatureCount()' always returns 0 at runtime, but when I set a breakpoint and step through the code I get the expected feature count for the layer of interest. Is there some race condition with counting?
QUESTION:
What am I doing wrong? Is there a better way to count the number of features having a non-zero attribute value in a specific map layer?
Solved! Go to Solution.
I was able to answer my own question. I needed to wait until the feature table loads before I check the count.
Correct code below:
LayerList mMapOperationalLayers = MainActivity.this.arcGISMap.getOperationalLayers(); // get map's LayerList for (final Layer layer : mMapOperationalLayers) { Log.d("layer", layer.getName()); if ((layer.getName().toUpperCase().equals("LAYER_OF_INTEREST")) { MainActivity.this.layer_of_interest = (FeatureLayer) layer; // save layer reference for later final FeatureTable featureTable = MainActivity.this.layer_of_interest.getFeatureTable(); featureTable.addDoneLoadingListener(new Runnable() { @Override public void run() { MainActivity.this.pages_count = featureTable.getTotalFeatureCount(); Log.d("loadMobileMapPackage", "pages_count:" + MainActivity.this.pages_count); } }); } // end if } // end for
I was able to answer my own question. I needed to wait until the feature table loads before I check the count.
Correct code below:
LayerList mMapOperationalLayers = MainActivity.this.arcGISMap.getOperationalLayers(); // get map's LayerList for (final Layer layer : mMapOperationalLayers) { Log.d("layer", layer.getName()); if ((layer.getName().toUpperCase().equals("LAYER_OF_INTEREST")) { MainActivity.this.layer_of_interest = (FeatureLayer) layer; // save layer reference for later final FeatureTable featureTable = MainActivity.this.layer_of_interest.getFeatureTable(); featureTable.addDoneLoadingListener(new Runnable() { @Override public void run() { MainActivity.this.pages_count = featureTable.getTotalFeatureCount(); Log.d("loadMobileMapPackage", "pages_count:" + MainActivity.this.pages_count); } }); } // end if } // end for