Android Question SetText of a view by using reflection

ArminKH

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

stevel05

Expert
Licensed User
Check the Java documentation, I think it should be java.lang.String ( capital S)
 

ArminKH

Well-Known Member
@stevel05
B4X:
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)
 

ArminKH

Well-Known Member
my project is
B4X:
#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
 

ArminKH

Well-Known Member
Why not call Label.Text = "my text" ?
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:
B4X:
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:

ArminKH

Well-Known Member
@Erel
Thats not possible by using reflection?thats worked by java object
 
Last edited:

ArminKH

Well-Known Member
You don't need reflection to do this.

This code will work with all views that are subclasses of TextView (which includes button):
B4X:
If v Is Label Then
Dim lbl As Label = v
lbl.Text = "Sdf"
End If
i know this erel
this is true but if i want to set text of different view my codes be a bit long
B4X:
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
 

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
 

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
 
Top