CustomListView - A flexible list based on ScrollView

Status
Not open for further replies.
Hello;
I used your costumview code. and I added 1label + 2spinner +1 checkbox in every panel.
So, I cant detecet /divide spinner1 or spinner2. And I want works spinner click each one.
How can I?

Code:
Sub Button1_Click
    ' scrool vieww için hazırlanmış
    '  https://www.b4x.com/android/forum/threads/class-customlistview-a-flexible-list-based-on-scrollview.19567/#content
    'https://www.b4x.com/android/forum/threads/scrollview-examples-summary.8423/#content
  
    ProgressDialogShow("Rehberiniz okunuyor ve listeleniyor...")
  
    Dim xcall As PhoneCalls
    Dim xsms As PhoneSms
    'xsms.Send("+905335135121","2 sistemm çalıstı")
    'StartActivity( xcall.Call("02127711379"))
    Dim c As Contact
    Dim cs As Contacts2
    '-------------Liste düzenleme-------------------
    clv3.Initialize(Me, "clv3")
    Activity.AddView(clv3.AsView, 0, 30%y, 100%x, 70%y)
    Dim phonelist As List
    phonelist.Initialize
    '----------------------------------
          Dim l As List
          l = cs.GetAll(True,True)
        Dim telf As Map
        'Msgbox(l.Size, "kişi sayısı")
          For i = 0 To l.Size - 1
            c = l.Get(i)
            telf= c.GetPhones
            Log(c.DisplayName)   
            'phonelist.AddAll(telf.Keys)
            phonelist.Clear
            For z=0 To telf.size-1
                phonelist.Add(telf.GetKeyAt(z))   
                'Log(z&". numara : " & telf.GetValueAt(z))
            Next
          
            'clv3.Add(CreateListItem2(c.DisplayName, clv3.AsView.Width, 80dip,phonelist), 80dip, "Item #" & i)
            clv3.Add(CreateListItem2(c.DisplayName, clv3.AsView.Width, 80dip,phonelist), 80dip, c.Id)
        Next 
    ProgressDialogHide
End Sub


Code:
'---- never run
Sub spn2_ItemClick (Position As Int, Value As Object)
    Msgbox("2 ci","")
End Sub
'always run it
Sub spn_ItemClick (Position As Int, Value As Object)
    Dim index As Int
    index = clv3.GetItemFromView(Sender)
    Dim pnl As Panel
    pnl = clv3.GetPanel(index)
    Dim spn,spn2 As Spinner
   
    Dim lbl As Label
    Dim chk As CheckBox
   
    lbl = pnl.GetView(1)
    spn = pnl.GetView(2)
    chk = pnl.GetView(0)
    spn2 = pnl.GetView(3)
    lbl.Text = "telfon!"
    Msgbox("Item value: " & clv3.GetValue(index) & CRLF & "spinner value: " & spn.GetItem(Position), "")
End Sub

Code:
Sub CreateListItem2(Text As String, Width As Int, Height As Int, phonelist As List) As Panel
    Dim p As Panel
    p.Initialize("")
    p.Color = Colors.Black
    Dim b As Button
    b.Initialize("button") 'all buttons click events will be handled with Sub Button_Click
    Dim chk As CheckBox
    chk.Initialize("chk")
    Dim lbl As Label
    lbl.Initialize("")
   
    Dim spn As Spinner
    Dim spn2 As Spinner
    spn.Initialize("spn")
    spn2.Initialize("spn")
    spn.AddAll(phonelist)
    spn2.AddAll(Array As String("Bey","Hanım","Amcacağım","Dayıcığım","Halacığım","Teyzeciğim", "Yengeciğim","Dedeciğim","Nineciğim","Ablacağım","Abiciğim","Kardeşim","Kuzenim","Yeğenim","babananeciğim", "anneanneciğim"))
    lbl.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    lbl.Text = Text
    lbl.TextSize = 16
    lbl.TextColor = Colors.White
    b.Text = "Click"
    'p.AddView( l - T w- h
    p.AddView(chk, 5dip, 2dip, 50dip, Height - 4dip) 'view #0
    p.AddView(lbl, 55dip, 2dip, 165dip, Height - 4dip) 'view #1
    p.AddView(spn, 220dip, 2dip, 170dip, Height - 4dip) 'view #2
    p.AddView(spn2, 390dip, 2dip, 150dip, Height - 4dip) 'view #3
    Return p
End Sub
 
There is a bug: DefaultTextBackgroundColor has no effect. It's because DefaultTextBackground = base.Background in DesignerCreateView() and later in InsertAtTextItem() the panel color is only set if DefaultTextBackground = Null.

A fix is to make DefaultTextBackground public and setting that to null in Activity_Create().

I also have a couple of questions:

1.
How can I remove the border (the white line) between the items?

2.
Is it possible to create a panel in the visual designer and use that panel in the CustomListView? I tried and got the exception:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

As I read it, the panel I created already has a parent and hence cannot be added in the InsertAt() sub. But there is no Activity.removeView() so how can I remove the view? Or is it simply not possible to design the panel visually? (It would be awesome if it was!)



 
Last edited:
1.
Code:
Dim sv As ScrollView = yourcustomlistview.AsView
sv.Color = ? 'insert here the same color used for your item panel and the divider will be invisible
or make "dividerHeight" public in CustomListView.bas and set it to 0.

2.
Code:
Dim pnl As Panel
pnl.Initialize("")
pnl.LoadLayout("yourlayout")
yourcustomlistview.InsertAdd(pnl, ...
 
Thanks, MaFu. #1 works fine!

#2:

A few questions:

A.
Do you mean .InsertAt() ?

B.
Should the designer actually contain a panel with the sub elements (labels, buttons etc) which is then loaded into pnl, or should I just add the elements directly to the designer since it ends up in pnl anyway?

C.
Do I need to create, initialize and load the layout for each element I need to add to the CLV? Or is it possible to sort of clone the panel and the just change the values of its child controls?
 
Thanks, MaFu. #1 works fine!

#2:

A few questions:

A.
Do you mean .InsertAt() ? yes, InsertAdd() was a typo.

B.
Should the designer actually contain a panel with the sub elements (labels, buttons etc) which is then loaded into pnl, or should I just add the elements directly to the designer since it ends up in pnl anyway?
panel is not needed, add the elements directly.

C.
Do I need to create, initialize and load the layout for each element I need to add to the CLV? Or is it possible to sort of clone the panel and the just change the values of its child controls?
yes, for each element.
 
I am still struggling with adding the the panel designed in the visual designer. It does not show up in the custom listview. This is what I do:

Code:
Sub Globals
    ...
    private clv As CustomListView
    ---
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ...
    For i = 0 To 10
        AddMyItem(i)
    Next
    ...
End Sub

Sub AddMyItem(i as integer)
        Dim p As Panel
        p.Initialize("")
        p.LoadLayout("5")
        clv.Add(p, 30, i)
End Sub

The Layout 5 simple consists of a single button with the text "Test" on.

If I click on the listview I can see the items get highlighted, but they don't show up. Like if the panel's layout is not loaded correctly or something.

If I instead create the panel manually, like this

Code:
Sub AddMyItem(i As integer)
        Dim p As Panel
        p.Initialize("")
        Dim btn As Button
        btn.Initialize("")
        btn.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.LEFT)
        btn.Text = "Test"
        btn.TextColor= Colors.Black
        p.AddView(btn, 0, 0, clv.AsView.Width, 50dip)
        btn.Height=50dip
        clv.Add(p, 50dip, i)
End Sub

it works.

So clearly I am missing something, but what?

I know I could just create the panel manually, but it's much more complicated than a single button :)
 
Are you using anchors or a script in this layout?

Anchors. Is that a problem?

It is not recommended to load a layout file to a panel with unknown size. Try to first add the panel to another panel just to set its size and then load the layout file.

Erel, I don't understand what you mean with that.

Edit: I am not sure if this is want you meant, but you gave me an idea and it seems to work!

Code:
Activity.AddView(p, 0,0,100%x,100%y)
p.LoadLayout("5")
p.RemoveView()
clv.Add(p, 75dip, item)

Adding the panel to the Activity and removing it again seems to do the trick. And I understand from you say that it will first get its dimensions when it's added to an activity or another panel.

Is this approach OK?
 
Last edited:
To change the order should work:

Yes, it does! Wow, I have spent ours trying to get it right, and all I needed was to change the order a bit :)

The good thing is that I learned from it, and I understand why the order is important: by adding the panel to the custom list view first, we ensure that it has a parent and hence also a fixed size. Precisely as Erel said. It's just a more clean way to simply add it to the clv to achieve this.
 
Yes, it does! Wow, I have spent ours trying to get it right, and all I needed was to change the order a bit :)
My fault, sorry for that. I've wrote my first example wrong.
 
hi
I checked the StringUtilse (version: 1.02) library , but I can't use this code :
Code:
Dim clv3 As CustomListView
what's problem on my app?
 

Attachments

  • Capture.PNG
    Capture.PNG
    91.9 KB · Views: 563
Last edited:
thanks MaF u:)
solved my problem
:Dactually I need add CustomListView.bas to the project file for use from CustomListView:D
thanks
 
Hi,
is there is a way to disable the whole list (make the items unclickable)?
I am using "clv1" only in my program.

thank you
 
Status
Not open for further replies.
Back
Top