Android Question customlistview sorting performance

Addo

Well-Known Member
Licensed User
i have a type of list that hold records that i will load it into CLV in order

B4X:
For i = 0 To Persons.Size - 1
Dim usr As Person
usr = Persons.Get(i)
If usr.nameid = unameid Then
Persons.RemoveAt(I)
Exit
End If
Next


Persons.SortType("nameid", True)



If clv.Size > 0 Then
clv.Clear
End If

For i = 0 To Persons.Size - 1
Dim usr As Person
Dim itmheigh As Int
usr = Persons.Get(i)

itmheigh = 38dip

clv.Add(createlistitem(usr.name&usr.nameid, clv.AsView.Width, itmheigh), usr.name&usr.nameid)
'Sleep(0)
Next

but i got a performance problems because of looping.
the phone lagging and freezing while adding the clv items because i have about 200 items

this code is requested any time with a callsub in tcp client Asyncstreamtext to remove some items from the clv and resort them.

i couldn't found any way to make this sorting better in performance sense every time i need to remove all items from clv and re add them to make the sorting, any help guys for better solution to have the same approach
 

Addo

Well-Known Member
Licensed User
B4X:
Sub createlistitem(Text As String, Width As Int, Height As Int) As Panel
    
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, Width, Height)
    p.LoadLayout("layoutlist")
    
    
    itmlbl.Text = text
    itmimage.Bitmap = LoadBitmap(File.DirAssets, "icon22.png")
    
    Return p
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User
itmimage.Bitmap = LoadBitmap(File.DirAssets, "icon22.png")
This should be outside createlistitem. ( you are loading the image every iteration, 200 times)
Create a global variable and load it once.
 
Upvote 0

Addo

Well-Known Member
Licensed User
itmimage.Bitmap = LoadBitmap(File.DirAssets, "icon22.png")
This should be outside createlistitem. ( you are loading the image every iteration, 200 times)
Create a global variable and load it once.
I will not use the same image this is A Simplified example , the image can be image from file or url or either not in dir assets at all
 
Upvote 0

Addo

Well-Known Member
Licensed User
This is not meant to be the same image this is a simplified example as I mentioned . I applied the example you have showing doesn't make a significant difference sense the problem here is always [clearing and add , clearing and add , clearing and add ]

The behaviour of continuously adding items if I want to sort again is the killer bullet
 
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User
In any case if I want to load other images
You should do it outside createlistitem(). Doing so will improve performance. Byitself CLV is slow loading items given the p.loadlayout() if you load every single image inside this iteration you are making it slower.
 
Upvote 0

Addo

Well-Known Member
Licensed User
You should do it outside createlistitem(). Doing so will improve performance. Byitself CLV is slow loading items given the p.loadlayout() if you load every single image inside this iteration you are making it slower.
Agree with loading image of slowing the addetion , and I know that already any how even if I remove the image at all in this example the main issue will still exist , too manny clearing and adding if I want to resort .

The image is not The issue here at all with it or without it the situation will remain the same , if I want to resort the items in clv I have to clear the clv and load all items again killing the app with loops .
 
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User
Another option would be to load the person list sorted once, then use clv.removeat() to delete and clv.insertat() to insert.
Note, you will only need to sort the person list and not the CLV items.
 
Upvote 0
Top