Printing the enum's name

27,263

Solution 1

For enumerations, you can obtain an array of all the constants and loop over them very easily using code such as this:

for(YourEnum value: YourEnum.values()){
    System.out.println("name="+value.name());
}

However, the Sensor class you link to isn't an enumeration, but contains a list of constants. There's no way to programatically loop over that list like an enumeration without specifying all the constants names.

However, you can create a static lookup that maps the ints to the String value you want to use, for example

Map<Integer,String> lookup = new HashMap<Integer,String>();
lookup.put(TYPE_ACCELEROMETER,"Accelerometer");
//Code a put for each TYPE, with the string you want to use as the name

You can use this like this:

Log.d("SENSORTYPE","Type: " + lookup.get(tempSensor.getType()));

This approach does mean you still have to write out each constant and update the list if the constants change, but you only have to do it once. It'd be a good idea to wrap the lookup in some kind of helper method or class depending on how widely you want to reuse it.

Solution 2

you can introduce an abstract method and implement it in every enumeration

enum Colour {

Red {

    @Override
    String colourName() {
        return "Red";
    }
};

abstract String colourName();
}

This way gives you more flexibility, for example, if you don't want to display its programmatic name

Solution 3

Log.d("SENSORNAME", "NAME: " + tempSensor.name());
Share:
27,263
vgonisanz
Author by

vgonisanz

Victor Goñi received his M.S. degree in Telecommunication Engeneering from the Public University of Navarra, Pamplona, Spain, in 2010. He spent an internship course at the Monterrey Institute of Technology and Higher Education, Mexico, during 2009, as a complement of his Engineer degree. He wrote his final Master Thesis at Vicomtech-IK4, San Sebastian, Spain, under the title of "Peripheral Evaluation for Virtual Environment Control", which received an A grade. Since then, he is working in the 3D Animation and Interactive Virtual Environments Department of Vicomtech-IK4 as a research assistant. He has developed a deep interest in science, programming and scientific research, especially in algorithm efficiency, real-time complex calculation and optimization. He is also interested in graphic engines and sensor data filtering and processing for electronic devices. He is specialized in code developement on mobile platforms. Also he is interested in resurrect tiranosaurios rex. My goal to achieve is to be a developer/architect that writes C++ like musicians compose music and songs.

Updated on May 15, 2020

Comments

  • vgonisanz
    vgonisanz almost 4 years

    I'm using eclipse + Android SDK on ubuntu.

    I would like to print the name of a sensor type device, there a A LOT OF THEM and i want to do it automatically.

    If i use a

    Log.d("SENSORTYPE","Type: " + tempSensor.getType())
    

    I print the (int) type, but i would like the name whom use the enum.

    How could i do that?

    Thants in advance.