Android Question CustomListView creation with code, not with designer

Status
Not open for further replies.

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi
I don't find documentation on how to programmatically create a CustomListView (using xCustomListView lib) and add it to a panel. I cannot use the designer in this case. I am adapting an existing "old style" ListView. Previous steps were Initialization and insertion on the panel. With this new view, things are different. For example, I have not clear the meaning of the CallBack request that IntelliSense proposes while initializing the CustomListView. An example should be welcome, or, at least a link to documentation.
Thanks in advance
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I declared a variable ListaCat as CustomListview. Then, when I try to Initialize, I get the request shown in the attached image.
 

Attachments

  • Immagine.png
    Immagine.png
    43.6 KB · Views: 452
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Hi
I don't find documentation on how to programmatically create a CustomListView (using xCustomListView lib) and add it to a panel. I cannot use the designer in this case. I am adapting an existing "old style" ListView. Previous steps were Initialization and insertion on the panel. With this new view, things are different. For example, I have not clear the meaning of the CallBack request that IntelliSense proposes while initializing the CustomListView. An example should be welcome, or, at least a link to documentation.
Thanks in advance
B4x CustomListView cannot be created at runtime.
[Callback object, generally speaking, is the object which will handle the events the... object (a View in this case) raises]
The solution is the one suggested by @mangojack
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi, Thanks for replies. But "can be loaded to a panel whenever it's needed" seems not so easy. Examples load CustomListView on Activity. Trying to add the CustomListView to a panel gives a compiler error.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi, no sorry, I did a little confusion, due to the fact that I am converting old code (due to your suggestion). Previously I had a Panel inside a CustomDialog , while now I have :

Dim pan As B4XView = xui.CreatePanel("")

I successfully converted the previous dialog, to use this new type of panel. It was fast and painless. But now I want to add a ListView which opens over the panel "pan", when the user clicks a button. Therefore I have declared a global CustomListView ListaCat:

Dim ListaCat as CustomListView

The error is:

pan.AddView(ListaCat,2%x,posy+deltay*2,96%x,30%y)
javac 1.8.0_121
src\b4a\identity\main.java:1664: error: incompatible types: customlistview cannot be converted to View

Thanks
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
BTW post #3 reports no link, if I don't miss something.
Before making a test project, let's do a last trial to resolve. I don't want you to waste more time.

I have now created, with designer, a Panel, with inside a CustomListView. The layout is named "Lista".

Then I declare, in Globals:
B4X:
 Private ListaCat As CustomListView
 Private ListaContainer As Panel
Inside the sub which displays the dialog, I have:
B4X:
Dim pan As B4XView = xui.CreatePanel("")
Then, as I understood, I must load the layout of ListaContainer, which contains the CustomListView ListaCat:
B4X:
 ListaContainer.LoadLayout("Lista")   ' Runtime error "Object should be first initialized"
  pan.AddView(ListaContainer,2%x,posy+deltay*2,96%x,30%y)

Now error is at Runtime: LoadLayout which says that Object must be initialized..
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
To create a custom listview via code without needing to use the designer, use the following code that was tested in b4a.

Declare a variable of type CustomListView in Globals
You will need to manually create a panel and a label to be used in creating the custom listview, in addition to creating a map with some default information for the custom listview.

You will need to call the Initialize and then DesignerCreateView functions of the custom listview


B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private CustomListView1 As CustomListView
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
   
    Dim Top As Int = 60dip
    Dim Left As Int = 0
    Dim Width As Int = Root.Width
    Dim Height As Int = (Root.Height - Top)
   
    Private mBaseCustomListView As B4XView
    Private mLabelCustomListView As B4XView
   
    mBaseCustomListView = xui.CreatePanel("")
    mBaseCustomListView.SetLayoutAnimated(0, 0, 0, Width, Height)
    mBaseCustomListView.Tag = ""
   
    mLabelCustomListView = XUIViewsUtils.CreateLabel
    mLabelCustomListView.SetLayoutAnimated(0, 0, 0, Width, Height)
   
    Dim props As Map
    props.Initialize
    props.Put("DividerColor", 0xFFD9D7DE)
    props.Put("DividerHeight", 2)
    props.Put("PressedColor", 0xFF7EB4FA)
    props.Put("InsertAnimationDuration", 300)
    props.Put("ListOrientation", "Vertical")
    props.Put("ShowScrollBar", True)
   
    CustomListView1.Initialize(Me, "CustomListView1")
    CustomListView1.DesignerCreateView(mBaseCustomListView, mLabelCustomListView, props)
   
    Root.AddView(mBaseCustomListView, Left, Top, Width, Height)
   
   
    Dim Names() As String = Array As String("John", "Joseph", "Daniel", "David")
    For i = 1 To 100
        Dim name As String = Names(Rnd(0, Names.Length)) & " " & i
        CustomListView1.AddTextItem(name, name)
    Next
End Sub

Private Sub CustomListView1_ItemClick (Index As Int, Value As Object)
    Log($"CustomListView1_ItemClick(${Index}, ${Value})"$)
End Sub


note:
the ideal is to use it by the designer as Erel indicates, because if in the future more properties are added to the customlistview, it may cause an error in the code
 
Last edited:
Upvote 0
Status
Not open for further replies.
Top