Android Question SetText of a view by using reflection

Discussion in 'Android Questions' started by ArminKH, Mar 1, 2015.

  1. ArminKH

    ArminKH Well-Known Member

    hi i want set the text of view by using reflection
    the solution in java object is
    Code:
    Dim T=lbl As JavaObject
        T.RunMethod(
    "setText",Array As Object("My Text"))
    i try following code but that not worked
    Code:
    Dim T As Reflector
        T.Target=lbl
        T.RunMethod2(
    "setText","My Text","java.lang.string")
    tnx all
     
  2. stevel05

    stevel05 Expert Licensed User

    Check the Java documentation, I think it should be java.lang.String ( capital S)
     
    ArminKH likes this.
  3. ArminKH

    ArminKH Well-Known Member

    @stevel05
    Code:
    Dim T As Reflector
        T.Target=lbl
        T.RunMethod2(
    "setText","My Text","java.lang.String")
    not worked again


    java.lang.NoSuchMethodException: setText [class java.lang.String]


    at java.lang.Class.getConstructorOrMethod(Class.java:460)
    at java.lang.Class.getDeclaredMethod(Class.java:685)
    at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
    at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod2(Reflection.java:817)
    at b4a.example.main._test(main.java:477)
    at b4a.example.main._runtest(main.java:435)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.keywords.Common$5.run(Common.java:962)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:153)
    at android.app.ActivityThread.main(ActivityThread.java:5341)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
    at dalvik.system.NativeStart.main(Native Method)
     
  4. ArminKH

    ArminKH Well-Known Member

    my project is
    Code:
    #Region  Project Attributes
        
    #ApplicationLabel: B4A Example
        
    #VersionCode: 1
        
    #VersionName:
        
    #SupportedOrientations: unspecified
        
    #CanInstallToExternalStorage: False
    #End Region
    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
    #End Region
    Sub Process_Globals
    End Sub
    Sub Globals
        
    Private Label1 As Label
    End Sub
    Sub Activity_Create(FirstTime As Boolean)
        Label1.Initialize(
    "")
            
    Activity.AddView(Label1,0,0,100%X,25%Y)
            CallSubDelayed2(Me,
    "Test",Label1)
    End Sub
    Sub Activity_Resume
    End Sub
    Sub Activity_Pause (UserClosed As Boolean)
    End Sub
    Public Sub Test(lbl As Label)
    Dim T As Reflector
        T.Target=lbl
        T.RunMethod2(
    "setText","My Text","java.lang.String")
       
    'Dim j=lbl As JavaObject
    '    j.RunMethod("setText", Array As Object("My Text"))
    End Sub
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Why not call Label.Text = "my text" ?
     
    ArminKH likes this.
  6. ArminKH

    ArminKH Well-Known Member

    Because i create a lib and i want to set text for each view it can be has text
    But user can select button and label or any other view
    For example i have a sub named:
    Code:
    Sub test(v as view)
    If v.tag="1" then
    Dim r as reflector
    R.target=v
    R.runmethod2(
    "setText","ok","java.lang.String")
    End if
    End sub
    Now the v parameter can be a label or button or edittext and thats not just label
    What is the solution?
     
    Last edited: Mar 1, 2015
  7. ArminKH

    ArminKH Well-Known Member

    @Erel
    Thats not possible by using reflection?thats worked by java object
     
    Last edited: Mar 1, 2015
  8. Erel

    Erel Administrator Staff Member Licensed User

    You don't need reflection to do this.

    This code will work with all views that are subclasses of TextView (which includes button):
    Code:
    If v Is Label Then
     
    Dim lbl As Label = v
     lbl.Text = 
    "Sdf"
    End If
     
    ArminKH likes this.
  9. ArminKH

    ArminKH Well-Known Member

    i know this erel
    this is true but if i want to set text of different view my codes be a bit long
    Code:
    If v Is Label Then
    Dim lbl As Label = v
    lbl.Text = 
    "Sdf"
    End If
    If v Is Button Then
    Dim btn As Button = v
    btn.Text = 
    "Sdf"
    End If

    AND........
    and one other problem is in widget where i want to set the text of label in widget

    if is possible please correct my reflection code
    tnx
     
  10. ArminKH

    ArminKH Well-Known Member

    this is possible by using java object lib but in my code i use reflection lib and i dont want use 2 same lib together
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Please read my previous answer again. The exact code I wrote (no need to duplicate it) will work with all subclasses of TextView. This includes Label, Button, EditText, CheckBox and RadioButton.
     
    ArminKH likes this.
  12. Erel

    Erel Administrator Staff Member Licensed User

    Note that there is no problem with using the two libraries together. It is not required here but still there is no problem at all.
     
    ArminKH likes this.
  13. ArminKH

    ArminKH Well-Known Member

    @Erel
    thats worked for label in a widget?
    i create a lib and if i use this 2 lib together users must be check 2lib thats better if all things done by using 1 lib
    all of my code used reflection lib and just this part not worked by using reflection
    ok thanks erel but i think thats better 4 all to know the correct code in above posts not just 4 me
    every thing can be done by many ways but this is not reason for forget learning other ways
    thanks again 4 your respons
     
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