B4J Question Calculate total price for checked Item (checkBox and Clv)

qey

Member
Hi everyone, I'm stuck on how to calculate the item price for every checked Item. This function user can check multiple item and the subtotal will automatically calculated.

B4X:
Private Sub CheckBox1_CheckedChange(Checked As Boolean)
    Dim cb As String
    Dim totalSelected As Int
    Dim chk As CheckBox = Sender.As(CheckBox)
    Dim itemC() As String = Regex.Split("\|", chk.Tag)
    If itemC.Length > 1 Or Checked == True Then
        chk.Tag = itemC(0)
        Return
    End If
    If Checked = True Then
        cb="1"
        lblDashOrderSubTotal.Text = lblDashOrderSubTotal.Text + priceItem
    Else
        cb="0"
    End If
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
There are some wrong things and some wrong choices.

Dim cb As String
What is that variable cb for? Is it a variable declared at module level (Activity/page)? If so, it must not be declared also within the sub.


If itemC.Length > 1 Or Checked == True Then
If itemC.Length > 1 Or Checked = True Then


lblDashOrderSubTotal.Text = lblDashOrderSubTotal.Text + priceItem
It is much better to add up between numeric variables and assign the (formatted) value to Views' text.


priceItem
Where do you get that value, priceItem?
I suppose that it has been entered by the user in each item or it is written in a Label of each item or it is the value associated with each item.
In the CheckedChange event you need to get it based on the above.
 
Upvote 0

qey

Member
There are some wrong things and some wrong choices.


What is that variable cb for? Is it a variable declared at module level (Activity/page)? If so, it must not be declared also within the sub.



If itemC.Length > 1 Or Checked = True Then



It is much better to add up between numeric variables and assign the (formatted) value to Views' text.



Where do you get that value, priceItem?
I suppose that it has been entered by the user in each item or it is written in a Label of each item or it is the value associated with each item.
In the CheckedChange event you need to get it based on the above.
For dim cb As String you can ignore it. I erase the block comment.

For priceItem, ive got it from here. it is the value associated with each item.

B4X:
Sub CreateProductListCLV
    Dim m As Map = UTILS.apiTokenMap
'    m.Put("item_id", item_id)
    Dim params As String
    params = UTILS.convertJson(m)
    Dim jobVerif As HttpJob
    jobVerif.Initialize("", Me)
    jobVerif.PostString(ApiUrl",params)
    jobVerif.GetRequest.SetContentType("application/json")
    Wait For (jobVerif) JobDone(Job As HttpJob)
'    Log(Job.GetString)
    If jobVerif.Success Then
        Dim parser As JSONParser
        parser.Initialize(Job.GetString)
        Dim RootCategory As List = parser.NextArray
        For Each colroot As Map In RootCategory
            Dim pnl As B4XView = xui.CreatePanel("")
            pnl.SetLayoutAnimated(0,0,0,CustomListViewItem.AsView.Width,CustomListViewItem.AsView.Height/4)
            pnl.LoadLayout("itemslayout")
            
            priceItem = colroot.Get("price")
            lblPrice.Text = Main.cLibs.FormatWithCurrency(priceItem)
            
            CustomListViewItem.Add(pnl, "")
        Next
    Else
        Toast.Show("AN ERROR OCCURED. PLEASE CHECK YOUR INTERNET CONNECTION !")
    End If
    jobVerif.Release
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Then you should get the lblPrice.Text in your CheckBox1_CheckedChange, but it is better if you write it here:

B4X:
CustomListViewItem.Add(pnl, priceItem)

B4X:
Private Sub CheckBox1_CheckedChange(Checked As Boolean)
    Dim chk As CheckBox = Sender.As(CheckBox)
    Dim ItemIndex As Int = CustomListViewItem.GetItemFromView(chk)
    Dim priceItem As Double = CustomListViewItem.GetValue(ItemIndex)
    Dim totalSelected As Double = lblDashOrderSubTotal.Text
    If chk.Checked Then
        totalSelected = totalSelected + priceItem
    Else
        totalSelected = totalSelected - priceItem
    End If
    lblDashOrderSubTotal.Text = NumberFormat2(totalSelected, 1, 2, 2, True)
End Sub


Not tested, wrote here directly.
 
Upvote 0

Intelemarketing

Active Member
Licensed User
Longtime User
Hi everyone, I'm stuck on how to calculate the item price for every checked Item. This function user can check multiple item and the subtotal will automatically calculated.

B4X:
Private Sub CheckBox1_CheckedChange(Checked As Boolean)
    Dim cb As String
    Dim totalSelected As Int
    Dim chk As CheckBox = Sender.As(CheckBox)
    Dim itemC() As String = Regex.Split("\|", chk.Tag)
    If itemC.Length > 1 Or Checked == True Then
        chk.Tag = itemC(0)
        Return
    End If
    If Checked = True Then
        cb="1"
        lblDashOrderSubTotal.Text = lblDashOrderSubTotal.Text + priceItem
    Else
        cb="0"
    End If
End Sub
Also, the obvious, if the Item is checked and then you UNCHECK it, you now need to Subtract the value (Using the above approach). There seem to be no quantities in the above example, but if you introduce a quantity as well, then you have a more complex problem to solve.
If you are checking a checkbox where you have only a very few items, then I would do a recalculation, of ALL the checked items, every time a checkbox is checked (or unchecked) - In this way you don't have a lot of decisions to make to arrive at a total, even with a quantity involved. (Hundreds of items in a list to pick from will not slow your program noticeably).
If on the other hand, you are dealing with a huge list (thousands of items) that could be checked or unchecked, I would create a separate list for the sale which would consist of the Item Code, Price and Quantity. Then if you check an item, you add it to the list, or remove it from the list if the item is unchecked - You would then cycle through the list to arrive at a total of checked items and quantities. (Presumably you are trying to calculate a value so that it can be displayed on the screen).

When you actually complete your list of checked items (ie, End of Sale), you may like to display the list of checked items on screen with totals etc.
Then you may need to make another series of calculations which may or may not include Taxes, Freight, etc, and possibly committing the quantities bought to reduce Stock on Hand, etc. when the Sale is finally committed.
 
Upvote 0
Top