Cursussen/Courses Codesnippets     Top 
B4A-lists - Items dialog


1. Layout
Go to the designer window and create a file called item_dialog.
Leave some room on the top for the dialog title bar. The second EditText field we will use for the notes per item. You can anchor this field to the bottom.
Don’t forget to generate the members with the fieldnames edt1 and edt2!


2. Code
Now you can write the dialog code. In the subroutine Class_Globals add a declaration for the dialog.
In the B4XPage_Created method add the initialization code.
' in Class_Globals
Public itemdialog As B4XDialog
' in B4XPage_Created
itemdialog.Initialize(Root)
itemdialog.PutAtTop = True
itemdialog.BackgroundColor = Colors.LightGray
itemdialog.BodyTextColor = Colors.Blue


3. Add
To add an item use the following code:
Sub btn1_Click
	If cbx1.Size < 1 Then
		Return
	End If
	If cbx2.Size < 1 Then
		Return
	End If
	edt1.Initialize("")
	Dim pnl1 As Panel = setup_dialog("item_dialog","Add item for " & cbx1.SelectedItem & " - " & cbx2.SelectedItem)
	Wait For (itemdialog.ShowCustom(pnl1, "OK", "", "CANCEL")) Complete (Result As Int)
	If Result = xui.dialogResponse_Positive Then
		If edt1.Text <> "" Then
			clv1.AddTextItem(edt1.text,edt1.text)
			If clv1.Size > 0 Then
				save_list(cbx1.Selecteditem,cbx2.Selecteditem)
			End If
		End If
	End If
End Sub


4. Change or delete
To change or delete an item use the following code.
Sub clv1_ItemClick (Index As Int, Value As Object)
	Dim pnl1 As Panel = setup_dialog("item_dialog","Edit/Delete category")
	Dim rs1 As ResumableSub =  itemdialog.ShowCustom(pnl1, "SAVE", "DELETE", "CANCEL")
	edt1.Text = clv1.GetValue(Index)
	Wait For (rs1) Complete (Result As Int)
	If Result = xui.dialogResponse_Positive Then
		If edt1.Text <> "" Then
			clv1.RemoveAt(Index)
			clv1.InsertAtTextItem(Index,edt1.text,edt1.text)
		End If
	else if Result = xui.dialogResponse_Negative Then
		clv1.RemoveAt(Index)
	End If
	If clv1.Size > 0 Then
		save_list(SelectedItem1,SelectedItem2)
	End If
End Sub


5. Wrapping up
Copy the setup_dialog subroutine from the CategoryPage.
Write some code for the switching of pages. When the page appears we clear the list, set the indexes and fill the first combobox. If the page disappears we save the list for the selected category and subcategory.
Now the app looks like this:
Private Sub B4XPage_Appear
	clv1.Clear
	SelectedIndex1 = 0
	SelectedIndex2 = 0
	FillComboBox1
End Sub

Private Sub B4XPage_Disappear
	If cbx1.Size > 0 Then
		If cbx2.Size > 0 Then
			save_list(cbx1.Selecteditem,cbx2.Selecteditem)
		End If
	End If
End Sub