For enumerations, you can easily get an array of all constants and a loop over them using the following code:
for(YourEnum value: YourEnum.values()){ System.out.println("name="+value.name()); }
However, the Sensor class you are referring to is not an enumeration, but contains a list of constants. There is no way to program a loop over this list as an enumeration without listing all constant names.
However, you can create a static search that maps ints to the String value that you want to use, for example
Map<Integer,String> lookup = new HashMap<Integer,String>(); lookup.put(TYPE_ACCELEROMETER,"Accelerometer");
You can use it as follows:
Log.d("SENSORTYPE","Type: " + lookup.get(tempSensor.getType()));
This approach means that you still have to write out each constant and update the list if the constants change, but you only need to do this once. It would be nice to wrap the search with some sort of helper method or class depending on how widely you want to reuse it.
chrisbunney
source share