Android Question Storing My Calculations

Shelby

Active Member
Licensed User
Hi All, Can you point me to the tutorial which tells me which storage object I should use to save my app's calculations? My app mathematically multiplies length times width and gives a Result. I'd like to save that result for adding it to the successive pages' results for a running total on each successive page.
Thanks
B4X:
Sub Globals
    'Variables always Private in this sub SSS
    Private Result As Double
    Private lblFind As Label
    Private lblResult As Label
    Private edtLen As EditText
    Private edtWidth As EditText
    Private btnCalc As Button
    Private pnlBitmap As Panel
 
    
    Private lblNext As Label
 
    Private lblTitle As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("math_layout")
    pnlBitmap.Initialize ("")
    Activity.AddView (pnlBitmap, 0%x, 270dip, 72%y, 190dip)
    Private bdwBitmap As BitmapDrawable
    bdwBitmap.Initialize(LoadBitmap(File.DirAssets, "Framing.jpg"))
    bdwBitmap.Gravity = Gravity.FILL
    pnlBitmap.Background = bdwBitmap
 
 
 
End Sub

Sub btnCalc_Click
    Result = edtLen.Text * edtWidth.Text
    lblResult.Text = Result & "     Square Feet"
    Log(Result & " Sq Ft")
End Sub


Sub lblNext_click
 
    StartActivity(Page2)
 
End Sub

'*******New idea for saving areas- but!... save in what receptacle?  Array? List?
Sub Save_Area
    Dim SvAry As Array
'or.........
    Dim SvLenandWid As List
    SvLenandWid.Initialize
 
 
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub
 
Last edited:

Shelby

Active Member
Licensed User
I think both. I want to save each result for use on the next page (which corresponds to the next room) and also add each successive result for using as a grand total. I'm trying to total all rooms in a building.
 

Shelby

Active Member
Licensed User
Thanks,
Now my problem is to find which of the tutorials might have explanations about how to use that KVS.... Collections? I have viewed all of the video tutorials but I don't recall where I might have read about the KVS.
 
Last edited:

DonManfred

Expert
Licensed User

emexes

Well-Known Member
Licensed User
No, I am two lazy. You haven't heard about my split personality.
Your homophonic humour made me smile, but I don't think it will translate well into German and so I can't "like" it. Manfred is a man of action, he'll point you in the right direction quicker than most everyone else here, but you gotto do the walking yourself.

Having said that, and getting back to topic:

How many rooms? Does the number of rooms vary? Is there other information about rooms (name, floor type, facilities, aircon, powerpoints, etc?) Will the final number of rooms be constant (eg, it's for your own home) or will you be resetting and starting again with new buildings (eg, it's for jobs that you do)?

Possible quick solutions to your two queries are:

1/ Save data in a file

For most apps, I save data in a Map (which is a list of Keys and their corresponding Values), eg, you might have:

B4X:
Dim Rooms As Map

Rooms.Initialize

Rooms.Put("NumRooms", 3)
Rooms.Put("Room1", "Bedroom")
Rooms.Put("Room1X", 2700)
Rooms.Put("Room1Y", 3300)
Rooms.Put("Room2", "Passage")
Rooms.Put("Room2X", 1800)
Rooms.Put("Room2Y", 5200)
Rooms.Put("Room3", "Kitchen")
Rooms.Put("Room3X", 2800)
Rooms.Put("Room3Y", 4200)

which you can then access eg:

B4X:
Dim TotalArea As Float = 0

Dim NumRooms As Int = Rooms.Get("NumRooms")
For I = 1 to NumRooms
    Dim RoomKey As String = "Room" & I

    Dim RoomName as String = Rooms.Get(RoomKey)
    Dim SizeX As Float = Rooms.Get(RoomKey & "X")
    Dim SizeY As Float = Rooms.Get(RoomKey & "Y")
    Dim Area As Float = (SizeX / 1000) * (SizeY / 1000)    'convert millimetres to metres at same time

    Log("Area of " & RoomName & " is " & NumberFormat2(Area, 1, 3, 0, False) & " m2")

    TotalArea = TotalArea + Area
Next

Log("Total area of " & NumRooms & " room(s) is " & NumberFormat2(Area, 1, 1, 1, False) & " m2")

Once you have that map, saving it in a file is easy:

B4X:
File.WriteMap(File.DirInternal, "RoomsAndMaybeOtherStuffTo.csv", Rooms)

or even better, save it in a directory that you can access from outside via FTP (eg, using B4ABridge):

B4X:
Dim MyDataDir As String = rp.GetSafeDirDefaultExternal("MyDataGoesHere")    'or any directory name you like
Dim MyDataFile As String = "RoomsAndMaybeOtherStuffTo.csv"
File.WriteMap(MyDataDir, MyDataFile, Rooms)

and then read it back eg:

B4X:
Dim MyDataDir As String = rp.GetSafeDirDefaultExternal("MyDataGoesHere")    'using save directory name as above
Dim MyDataFile As String = "RoomsAndMaybeOtherStuffTo.csv"

Dim EmptyRooms As Map
EmptyRooms.Initialize
EmptyRooms.Put("NumRooms", 0)

If File.Exists(MyDataDir, MyDataFile) Then
    Rooms = File.ReadMap2(MyDataDir, RoomsAndMaybeOtherStuffTo.csv, EmptyRooms)
Else
    Rooms = EmptyRooms    'if first time app run and no file exists, then start with empty list of rooms
End If

2/ Share data across activities

by putting the above Rooms map (and any other data that you want to share between screens/activities) in the Process_Globals of a module that doesn't come and go like activities do. The obvious choice is the Starter module that is now created by default when you start a new project:

B4X:
Sub Process_Globals

    Dim Rooms As Map    'public by default (or you can write "Public Rooms As Map" if you'd nee... I mean: like, a reminder)

End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.

    Rooms.Initialize
    Rooms.Put("NumRooms", 0)    'another obvious place to put the first-app-run default

    ReadRoomData(Rooms)

End Sub

and then you can access it anywhere within the Starter module with:

B4X:
Log( Rooms.Get("NumRooms") )

or from any other module with:

B4X:
Log( Starter.Rooms.Get("NumRooms") )
 
Last edited:

Shelby

Active Member
Licensed User
The point is to continue calculating the areas of rooms until there are none left to calculate. That means the number of rooms will always be an unknown until Mr. User finishes calculating.
 

Shelby

Active Member
Licensed User
Will the final number of rooms be constant (eg, it's for your own home) or will you be resetting and starting again with new buildings (eg, it's for jobs that you do)?
Alway be changing.
 

Shelby

Active Member
Licensed User
Dim Rooms As Map

Rooms.Initialize

Rooms.Put(
"NumRooms", 3)
Rooms.Put(
"Room1", "Bedroom")
Rooms.Put(
"Room1X", 2700)
Rooms.Put(
"Room1Y", 3300)
Rooms.Put(
"Room2", "Passage")
Rooms.Put(
"Room2X", 1800)
Rooms.Put(
"Room2Y", 5200)
Rooms.Put(
"Room3", "Kitchen")
Rooms.Put(
"Room3X", 2800)
Rooms.Put(
"Room3Y", 4200)
Are the x's and y's with corresponding numbers relating to the position in the map (storage file)? I'm just trying to understand how this works. Or.... Maybe they stand for the individual square footages or lengths and widths?
 

emexes

Well-Known Member
Licensed User
The point is to continue calculating the areas of rooms until there are none left to calculate. That means the number of rooms will always be an unknown until Mr. User finishes calculating.
Somewhere I got the impression you were going to have a separate screen for each room. Or maybe you just said something about multiple screens, and I extrapolated too far.

Anyway, if it's just a calculator like that, couldn't you have it like the old-style adding machines, where you press [Clear] to start a new calculation, and then for each room: fill in the width and length fields, then press [Add] to calculate the area and add it to a grand count and total that is displayed on the same screen eg "6 rooms = 18.4 m2"?

And perhaps have a [Undo] button to remove the last room from the total? (previous example becomes eg "5 rooms = 12.7 m2")
 

Shelby

Active Member
Licensed User
O
Dim RoomName as String = Rooms.Get(RoomKey)
Dim SizeX As Float = Rooms.Get(RoomKey & "X")
Dim SizeY As Float = Rooms.Get(RoomKey & "Y")
Dim Area As Float = (SizeX / 1000) * (SizeY / 1000) 'convert millimetres to metres at same time

Log("Area of " & RoomName & " is " & NumberFormat2(Area, 1, 3, 0, False) & " m2")
O.K. it looks like they are the lengths and widths that one works with.
 

emexes

Well-Known Member
Licensed User
Are the x's and y's with corresponding numbers relating to the position in the map (storage file)? I'm just trying to understand how this works. Or.... Maybe they stand for the individual square footages or lengths and widths?
X and Y are the orthagonal dimensions of the (presumably rectangular) room. I used those to avoid the which-is-which dilemma of a square(ish) room.

But then later I followed your lead to using more relaxed terminology ;-)
 

Shelby

Active Member
Licensed User
Well I want to bring forward the previous room (s), calculate the new room and add the two.
 

emexes

Well-Known Member
Licensed User
Isn't it past your bedtime over there? Although I did notice that daylight savings has abandoned us here, like it's still dark at 7am, so... maybe not.
 

Shelby

Active Member
Licensed User
X and Y are the orthagonal dimensions of the (presumably rectangular) room. I used those to avoid the which-is-which dilemma of a square(ish) room.
Right, no dilemma for now. Odd shaped rooms will eventually be a consideration.
 

Shelby

Active Member
Licensed User
I go to bed in 45 minutes to get up at 5 am tomorrow. In 45 mins it will be 9 pm here.
 

emexes

Well-Known Member
Licensed User
Dare I ask what units you're going to be using? I've been trying to lead you to the Nirvana of Metric, but last I saw, you guys were still firmly Imperial.
 
Top