Android Code Snippet Access ENum's

Discussion in 'Code Snippets' started by stevel05, Feb 28, 2015.

  1. stevel05

    stevel05 Expert Licensed User

    Sub Name: ENumClass

    Description: This Class allows access to Java Enum's. If you're dabbling with JavaObject, it won't be too long before you come across an ENum. Some methods that require Enums will accept Strings, but others insist on the Enum Object. With this class you can get the Enum Object.

    Code:
    'Class module
    Sub Class_Globals
        
    Private EnumJO As JavaObject
    End Sub

    'Initializes the object. You can add parameters to this method if needed.
    Public Sub Initialize(ClassName As String)
        EnumJO.InitializeStatic(ClassName)
    End Sub
    'Return the Constant for a given Constant String Text
    Sub ValueOf(Text As StringAs Object
        
    Return EnumJO.RunMethod("valueOf",Array(Text))
    End Sub
    'Returns an array containing the constants of this enum type, in the order they are declared.
    Sub Values As Object()
        
    Return EnumJO.RunMethod("values",Null)
    End Sub
    'Returns an array containing a string representation of the constants of this enum type, in the order they are declared.
    Sub ValueStrings As String()
        
    Dim ValueObjects() As Object = Values
        
    Dim JO As JavaObject
        
    Dim ReturnStrings(ValueObjects.Length) As String
        
    For i = 0 To ValueObjects.Length - 1
            JO = ValueObjects(i)
            ReturnStrings(i) = JO.RunMethod(
    "toString",Null)
        
    Next
        
    Return ReturnStrings
    End Sub
    Usage:
    Code:
    Dim PaintStyle As ENumClass
        PaintStyle.Initialize(
    "android.graphics.Paint.Style")
        
    Log(PaintStyle.ValueOf("FILL"))
        
    Log(GetType(PaintStyle.ValueOf("FILL")))

    Depends On: JavaObject

    Tags: Enum B4a
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Can you post an example that didn't work with JavaObject?

    This code works:
    Code:
    Dim paint As JavaObject
    paint.InitializeNewInstance("android.graphics.Paint"Null)
    'set PaintStyle enum
    paint.RunMethod("setStyle"Array("FILL"))
     
  3. stevel05

    stevel05 Expert Licensed User

    Yes, most of the enums accept strings, I can't find the example had, I was testing loads of enums it may have been in a 3rd party jar, but it definitely needed an object. When (if) I come across it, or another example I'll post it here.
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice