Android Question [B4X] [XUI] Issue with xCustomListView with TextChanged event

Joey249

Member
I've added in my custom controls as followed from Erel's Tutorial, everything is working as expected but when I try and grab the controls from a TextChanged event B4xpages always crashes with an error message. The code i'm trying to use works perfectly in other areas, it just has an issue with the TextChanged event as I presume i'm doing it incorrectly for that event.

Here's the code for that segment
B4X:
Private Sub TxtServiceQty_TextChanged (Old As String, New As String)
    Dim index As Int = ListViewServices.GetItemFromView(Sender)
    Dim iv As ItemValue = ListViewServices.GetValue(index)
    iv.LblTotalPrice.text = iv.TxtServicePrice.Text * iv.TxtServiceQty.text
End Sub

This is the way Erel demonstrated the usage of the code for a button click event at 16:39 in his video, however i'm sure a text event is different i'm just not quite sure how to go about it, i've tried a For loop which works but then it also has to update all other controls which don't need updating all the time and causes a delay as a result of it.
B4X:
Private Sub TxtServiceQty_TextChanged (Old As String, New As String)
    For i = 0 To ListViewServices.Size - 1
        Dim iv As ItemValue = ListViewServices.GetValue(i)
        iv.LblTotalPrice.text = iv.TxtServiceQty.Text * iv.TxtServicePrice.Text
    Next
End Sub

If anyone has any examples on how to go about this it would be greatly appreciated.

Error Message while loading the page:
B4X:
Error occurred on line: 69 (TestServicePage)
java.lang.ClassCastException: b4a.example.b4xfloattextfield cannot be cast to android.view.View
    at anywheresoftware.b4a.objects.B4XViewWrapper.asViewWrapper(B4XViewWrapper.java:91)
    at anywheresoftware.b4a.objects.B4XViewWrapper.getParent(B4XViewWrapper.java:188)
    at b4a.example3.customlistview._getitemfromview(customlistview.java:413)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1114)
    at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:1077)
    at b4a.example.b4xfloattextfield._tf_textchanged(b4xfloattextfield.java:1145)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.objects.EditTextWrapper$1.afterTextChanged(EditTextWrapper.java:83)
    at android.widget.TextView.sendAfterTextChanged(TextView.java:7424)
    at android.widget.TextView.setText(TextView.java:3822)
    at android.widget.TextView.setText(TextView.java:3671)
    at android.widget.EditText.setText(EditText.java:80)
    at android.widget.TextView.setText(TextView.java:3646)
    at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
    at anywheresoftware.b4a.objects.B4XViewWrapper.setText(B4XViewWrapper.java:229)
    at b4a.example.b4xfloattextfield._settext(b4xfloattextfield.java:886)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at b4a.example.testservicepage._b4xpage_created(testservicepage.java:85)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
    at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:285)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1087)
    at b4a.example.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:1070)
    at b4a.example.b4xpagesmanager._showpage(b4xpagesmanager.java:427)
    at b4a.example.b4xpages._showpage(b4xpages.java:44)
    at b4a.example.createappointmentpage._btnaddservice_click(createappointmentpage.java:121)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at andr
Message longer than Log limit (4000). Message was truncated.
 
Solution
The answer is that your are trying to use a control which is not actually a view in getItemFromView

try using
B4X:
Private Sub TxtServiceQty_TextChanged (Old As String, New As String)
private mycustomview as b4xtextfloatfield = sender ' I am assuming this is the correct control from the error message, it may not be
    Dim index As Int = ListViewServices.GetItemFromView(mycustomview.mbase) ' use a real view rather than the custom control object
    Dim iv As ItemValue = ListViewServices.GetValue(index)
    iv.LblTotalPrice.text = iv.TxtServicePrice.Text * iv.TxtServiceQty.text
End Sub

There are posts on the forum which talk about this issue.

Basically, a custom control is not a view.

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
The answer is that your are trying to use a control which is not actually a view in getItemFromView

try using
B4X:
Private Sub TxtServiceQty_TextChanged (Old As String, New As String)
private mycustomview as b4xtextfloatfield = sender ' I am assuming this is the correct control from the error message, it may not be
    Dim index As Int = ListViewServices.GetItemFromView(mycustomview.mbase) ' use a real view rather than the custom control object
    Dim iv As ItemValue = ListViewServices.GetValue(index)
    iv.LblTotalPrice.text = iv.TxtServicePrice.Text * iv.TxtServiceQty.text
End Sub

There are posts on the forum which talk about this issue.

Basically, a custom control is not a view.
 
Upvote 1
Solution

Mahares

Expert
Licensed User
Longtime User
I think DigitWell means B4XFloatTextField instead b4xtextfloatfield . I wanted to point that out because the OP is new and we do not want to confuse him.
If you still need help, upload your project for others to check out. But, DIgitWell 's answer seems the most plausible based on your error message
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I think DigitWell means B4XFloatTextField instead b4xtextfloatfield . I wanted to point that out because the OP is new and we do not want to confuse him.
If you still need help, upload your project for others to check out. But, DIgitWell 's answer seems the most plausible based on your error message
You are correct. Sorry for the typo.
 
Upvote 0

Joey249

Member
The answer is that your are trying to use a control which is not actually a view in getItemFromView

try using
B4X:
Private Sub TxtServiceQty_TextChanged (Old As String, New As String)
private mycustomview as b4xtextfloatfield = sender ' I am assuming this is the correct control from the error message, it may not be
    Dim index As Int = ListViewServices.GetItemFromView(mycustomview.mbase) ' use a real view rather than the custom control object
    Dim iv As ItemValue = ListViewServices.GetValue(index)
    iv.LblTotalPrice.text = iv.TxtServicePrice.Text * iv.TxtServiceQty.text
End Sub

There are posts on the forum which talk about this issue.

Basically, a custom control is not a view.
That worked perfectly and explanation made sense, thank you very much!
 
Upvote 0

Joey249

Member
I think DigitWell means B4XFloatTextField instead b4xtextfloatfield . I wanted to point that out because the OP is new and we do not want to confuse him.
If you still need help, upload your project for others to check out. But, DIgitWell 's answer seems the most plausible based on your error message
DigitWell's solution worked perfectly but thank you! New to BJA but used to vb.net, just taking a little time wrapping my head around the different ways of setting everything up and slightly different way of coding it.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
DigitWell's solution worked perfectly but thank you! New to BJA but used to vb.net,
I can tell from your initial post that you are a seasoned programmer, although you just joined B4X. Here is another way of expressing the answer code to your question above, using the new '.as'
B4X:
Private Sub TxtServiceQty_TextChanged (Old As String, New As String)
  Dim index As Int = ListViewServices.GetItemFromView(Sender.As(B4XFloatTextField).mBase)
  Dim iv As ItemValue = ListViewServices.GetValue(index)
  iv.LblTotalPrice.text = iv.TxtServicePrice.Text * iv.TxtServiceQty.text
 End Sub
 
Upvote 0
Top