Cursussen/Courses Codesnippets     Top 
B4A-lists - Category page


1. Show layout
Make sure you select the CategoryPage tab!
Change the title on the menubar:
Private Sub B4XPage_Created (Root1 As B4XView)
	Root = Root1
	Root.LoadLayout("category_layout")
	B4XPages.SetTitle(Me,"Lists_textfiles_notes - Category")
End Sub


2. Declarations
Declare variables for the layout views. This can be done from the designer. Open the designer window and select in the menu File the category_layout entry. Then click on Tools / Generate Members.
Check the boxes for btn2 and its Click event and for clv2 and its ItemClick event. Then click on the “Generate Members” button. This action will add code to the CategoryPage tab in the IDE main window:
Sub Class_Globals
	…	
	Private btn2 As Button
	Private clv2 As CustomListView
End Sub

Sub clv2_ItemClick (Index As Int, Value As Object)
	
End Sub

Sub btn2_Click
	
End Sub


3. Fill listview
To add all items to the custom listview we write a small subroutine. Don’t forget to initialize the local list (private lst as List)!
This subroutine will be called in the B4Xpage_Appear method. First we clear the list and then we load the items.
Sub load_list
	Private lst As List
	lst.Initialize
	' add test items (remove after testing)
	For i = 0 To 3
		clv2.AddTextItem("item " & i, "item " & i)
	Next	
End Sub

Private Sub B4XPage_Appear
	clv2.Clear
	load_list
End Sub


4. Layout events
By clicking on an item in the custom listview a message is shown giving information about the item. This is done in the subroutine clv2_ItemClick.
If you want to add more items then you can write some code in the btn2_Click subroutine.
Note: to put a text on the button go to the designer window, click on the button and type in the property text the word “Add”. Save the layout file and the next time the button has a text Add on it.
Sub clv2_ItemClick (Index As Int, Value As Object)
	ToastMessageShow("item at position " & Index & " has value " & Value,True)
End Sub

Sub btn2_Click
	clv2.AddTextItem("item " & clv2.Size, clv2.Size)
End Sub


5. Write to file
To write the list to a file you can use the class File and its methods Delete and WriteList. To update the list the file is first deleted and then written.
The list is saved in the file “category.txt” in the internal directory from the app.
This code will be called in the method B4Xpage_Disappear. When you switch between pages this method is called before leaving the page so this is a good place to do some updating.
This code will of course also be called when you add a category to the list.
Sub save_list
	Private lst As List
	lst.Initialize
	For i = 0 To clv2.Size-1
		lst.Add(clv2.GetValue(i))
	Next
	File.Delete(File.DirInternal,"category.txt")
	File.WriteList(File.DirInternal,"category.txt",lst)
End Sub

Private Sub B4XPage_Disappear
	If clv2.Size > 0 Then
		save_list
	End If
End Sub

Sub btn2_Click
	clv2.AddTextItem("item " & clv2.Size, clv2.Size)
	If clv2.Size > 0 Then
		save_list
	End If

End Sub


6. Read from file
The load_list subroutine has to be changed accordingly.
Sub load_list
	Private lst As List
	lst.Initialize
	If (File.Exists(File.DirInternal,"category.txt")) Then
		lst = File.ReadList(File.DirInternal,"category.txt")
	End If
	For i = 0 To lst.Size-1
		clv2.AddTextItem("item " & lst.Get(i), lst.Get(i))
	Next
	' add temporally test items (remove after testing)
'	For i = 0 To 3
'		clv2.AddTextItem("item " & i, i)
'	Next	
End Sub


7. Third run
To make sure you start with an empty list you have to remove the app from the device and then run the app from the B4A IDE.