Cursussen/Courses Codesnippets     Top 
B4A-lists - Subcategory page


1. Layout
Click on the SubcategoryPage tab in the IDE. Open the designer window and open the subcategory_layout file.
Generate the members of this layout:
Change the text for the button to “Add”.


2. Code
Go back to the main window and copy and paste the code from the CategoryPage tab to the SubcategoryPage tab. Change category into subcategory where needed. Clv2 becomes clv3 and btn2 becomes btn3.
Copy the methods B4XPage_Appear, B4XPage_Disappear, setup_dialog, load_list, save_list.
Note that the IDE will underline in red where there needs to be a change in the code.
The list will be saved in the “subcategory.txt” file.
Copy the code from btn2_Click in CategoryPage to btn3_Click in SubcategoryPage and adjust for subcategory. Don’t change the “category_dialog” string as we use the same layout as for category.
Copy the code from clv2_ItemClick in CategoryPage to clv3_ItemClick in SubcategoryPage and adjust for subcategory. Don’t change the “category_dialog” string as we use the same layout as for category.
The subcategory list should work now.


3. Combobox
Now we need to fill the combobox with the category list. This is the code:
We read the “category.txt” file, set the combobox items for this list and keep track of the selected index and the selected item in the combobox for use in the load_list and save_list methods.
We call the FillComboBox method in the B4Xpage_Appear subroutine.
This is what it should look like now:
Public Sub FillComboBox
	Private lst As List
	lst.Initialize
	If (File.Exists(File.DirInternal,"category.txt")) Then
		lst = File.ReadList(File.DirInternal,"category.txt")
	End If
	'lst.SortCaseInsensitive(True)
	cbx3.SetItems(lst)
	cbx3.SelectedIndex = 0
	SelectedIndex = 0
	If lst.Size > 0 Then
		SelectedItem = cbx3.GetItem(SelectedIndex)
	End If
End Sub

Private Sub B4XPage_Appear
	clv3.Clear
	FillComboBox
	load_list
End Sub


4. List read or write
The subcategory list belongs to 1 category that is selected in the combobox.
So the save_list and load_list subroutines need to be changed.
If the user changes the selected category we need to save the list and load the new list.
Sub load_list
	Private lst As List
	lst.Initialize
	If (File.Exists(File.DirInternal,SelectedItem & "_subcategories.txt")) Then
		lst = File.ReadList(File.DirInternal,SelectedItem & "_subcategories.txt")
	End If
	For i = 0 To lst.Size-1
		clv3.AddTextItem(lst.Get(i), lst.Get(i))
	Next
End Sub

Sub save_list
	Private lst As List
	lst.Initialize
	For i = 0 To clv3.Size-1
		lst.Add(clv3.GetValue(i))
	Next
	File.Delete(File.DirInternal,SelectedItem & "_subcategories.txt")
	File.WriteList(File.DirInternal,SelectedItem & "_subcategories.txt",lst)
End Sub

Sub cbx3_SelectedIndexChanged (Index As Int)
	save_list
	SelectedIndex = Index
	SelectedItem = cbx3.SelectedItem
	clv3.Clear
	load_list
End Sub


5. Wrapping up
In the label of the dialog you can add the name of the category that the subcategory belongs to.
For this you change the lines for the setup of the dialog:
Dim pnl1 As Panel = setup_dialog("category_dialog","Add subcategory to " & cbx3.SelectedItem)
Dim pnl1 As Panel = setup_dialog("category_dialog","Edit/Delete subcategory from " & cbx3.SelectedItem)