Cursussen/Courses Codesnippets     Top 
B4A-budget - wrapping up


1. functionalities check
When changing an expenses or earnings record the category_id field is not filled correctly.
So far we have been using the rowid "1" for the category_id. Look at the lines:
In show_add_dialog:
			add_record(strtablename, Array As String(lbldate.Text,"1",ftf2.Text,ftf1.Text))
In show_change_dialog:
			change_record(strtablename, Array As String(lbldate.Text,"1",ftf2.Text,ftf1.Text),rowid)
In the datepicker_dialog:
			If old_amount = "" Then
				add_record(strtablename, Array As String(lbldate.Text,"1",ftf2.Text,ftf1.Text))
			Else
				change_record(strtablename, Array As String(lbldate.Text,"1",ftf2.Text,ftf1.Text),1)
			End If
These lines need to be changed to the correct category_id (= rowid from the category table).
The correct lines are:
			add_record(strtablename, Array As String(lbldate.Text,cbx1rowids.Get(SelectedIndex),ftf2.Text,ftf1.Text))
			change_record(strtablename, Array As String(lbldate.Text,cbx1rowids.Get(SelectedIndex),ftf2.Text,ftf1.Text),rowid)
				If old_amount = "" Then
					add_record(strtablename, Array As String(lbldate.Text,cbx1rowids.Get(SelectedIndex),ftf2.Text,ftf1.Text))
				Else
					change_record(strtablename, Array As String(lbldate.Text,cbx1rowids.Get(SelectedIndex),ftf2.Text,ftf1.Text),upd_rowid)
				End If
Now the category_id is filled with the value from the selected item in the cbx1 combobox.
Here is an example:
The date selection is not completed yet. You can select a date before the start date or after the end date. This should not be allowed. And selecting a date past the current date should not be allowed.
Add these lines in the datepicker_dialog below the test for the response positive:
	If Result = xui.dialogResponse_Positive Then
		If cal.lblticks > DateTime.Now Then
			xui.MsgboxAsync("Only today and past dates are allowed!",strtablename)
			Return
		End If
		If cal.lblticks < strstartticks Then
			xui.MsgboxAsync("The date is earlier than the starting date!",strtablename)
			Return
		End If
		If cal.lblticks > strendticks Then
			xui.MsgboxAsync("The date is later than the end date!",strtablename)
			Return
		End If
Now the user can only select a date within the range from the start date until the end date. The selected date can not be in the future.
Note: make sure the variables strstartticks and strendticks are initialized (in B4XPage_Created).
	strstartticks = DateUtils.SetDate(curryear,currmonth,1)
	strendticks = DateUtils.SetDate(curryear,currmonth,DateUtils.NumberOfDaysInMonth(currmonth,curryear))
EDIT: And also make sure you get the right settings for these variables! Add the lines about the date settings in the code from the B4XMainPage tab:

private Sub B4XPage_Appear
	lblbalance.Text = 0
	strcurrency = "EUR"
	If db.get_num_rows("settings") > 0 Then
		Private sql As String = "SELECT rowid, startdate, enddate, startbalance,currency FROM settings"
		Private rs1 As ResultSet = db.select_query(sql)
		rs1.NextRow
		' get date settings
		DateTime.DateFormat = "yyyy-MM-dd"
		strstartticks = DateTime.DateParse(rs1.GetString("startdate"))
		strendticks = DateTime.DateParse(rs1.GetString("enddate"))
		Log(strstartticks)
		Log(strendticks)
		' get balance and currency settings
		lblbalance.Text = rs1.GetString("startbalance")
		strcurrency = rs1.GetString("currency")
	End If
...


2. Check the layout
The app is nearly finished. The look of the labels can make the app more appealing.
The balance label with the currency between brackets and the showing of the start balance can be improved.
Go to the designer window and open the MainPage layout file.
Click on the lblcurrency label and check the text properties:
Style = BOLD, Vertical Alignment = TOP, Size = 18.
In the common properties:
Padding = 3, 0, 3, 0.
Click on the lblbalance label and change some properties:
Padding = 3, 0, 3, 0.
Style = BOLD, Horizontal Alignment = RIGHT, Size = 20, Text color = #FF000000
Drawable properties:
Color = #FFFCF69C, Corner radius = 5, Border width = 2
Open the tab1_layout file for the expenses look.
Click on the lbltotalexpense and change some properties:
Padding = 3, 0, 3, 0.
Style = BOLD, Horizontal Alignment = RIGHT, Size = 18, Text color = #FFFF0000
Color = #FFFFDFDD, Corner radius = 5, Border width = 2
Click on the sbtn1 and check the colors in the CustomView properties:
Primary color = #FFF11B00, secondary color = #FFB01702.
Open the tab2_layout file for the earnings look.
Click on the lbltotalearning and change some properties:
Padding = 3, 0, 3, 0.
Style = BOLD, Horizontal Alignment = RIGHT, Size = 18, Text color = #FF19D504
Color = #FFD9FFD0, Corner radius = 5, Border width = 2
Click on the sbtn2 and check the colors in the CustomView properties:
Primary color = #FF1EF100, secondary color = #FF2D8F10.
This looks like:


3. Compile and run
Time to test the app.
Compile and run the app.
You should reset the database for the first run (see section database changes - 3. B4XMainPage code).
You can't add expenses or earnings if you don't add categories.
To inform the user of this you could add a test in the B4XPage_Created to check if the table category contains records.
You can do that with these lines of code:
	If db.table_exists("category") Then
		If db.get_num_rows("category") < 1 Then
			xui.MsgboxAsync("First add categories!", "Mybudget")
		End If
	End If
If you don't like the grey background color of the db_dialog layout then you go to the designer and open the file db_dialog.
Click in the Views Tree pane on the Activity and change in the Properties pane the Drawable Color to white (#FFFFFFFF).


4. Assignment
In the B4A-lists tutorial the user can change the order of the category or subcategory list items.
This is done by the dragger class.
The changed order has an effect on the display of the combobox items.
In this tutorial the CategoryPage also contains a dragger class that is used to change the order of the category list items.
It is not essential but it would be a nice feature to have if the cbx1 combobox displays the items in the same order as in the CategoryPage list. And of course that is the reason why we use the dragger class in the CategoryPage.
Can you implement a solution for that problem?