How can I speed up filling this TListBox?

  

Problem

I have 2 different json arrays that looks like this:

1 – Events List

{“0000:First Event Name”,”0001:Second Event Name”,”0002:Third Event Name”}

2 – Available Events List

{“0001″,”0002″}

and then I need to generate a ListBox with the available events using checkboxes:

[ ] First Event Name
[x] Second Event Name
[x] Third Event Name

Given solution

procedure TFormHome.GetEvents(Sender: TObject);
var

K: Integer;
Z: Integer;
ListCount_Events : Integer;
AvailableList_Count : Integer;

lb_item: TListBoxItem;

event_code : string;
event_code_1: string;
event_name : string;

begin

// Check if the JSON responses are not nil
if ((json_response_events <> nil) and (json_response_available_events <> nil)) then
begin

ListCount_Events := json_response_events.Count;

// Get Available List Count
AvailableList_Count := json_response_available_events.Count;

try
// Run a for loop to create the events based on ListCount_Events
for K := 0 to (ListCount_Events – 1) do
begin

// Get complete Event Code
event_code_1 := StringReplace(json_response_events.Items[K].ToString.Split([‘:’])[0], ‘”‘, ”, [rfReplaceAll]);

// Get complete Event Name
event_name := StringReplace(json_response_events.Items[K].ToString.Split([‘:’])[1], ‘”‘, ”, [rfReplaceAll]);

// Create the ListBoxItem
lb_item := TListBoxItem.Create(self);

// Assign it to the the ListBox component
lb_item.Parent := lb_notifications;

// ListBoxItem get the event name
lb_item.Text := event_name;

// Remove StyledSettings (Other)
lb_item.StyledSettings := lb_item.StyledSettings – [TStyledSetting.Other];

// Remove StyledSettings (FontColor)
lb_item.StyledSettings := lb_item.StyledSettings – [TStyledSetting.FontColor];

// Change TextSettings FontColor to default
lb_item.TextSettings.FontColor := $FF626262;

// Set selectable to false in order to not permit the user
// to select multiple items on the List
lb_item.Selectable := false;

// Set the appropriated style
lb_item.StyleLookup := ‘listboxitemleftdetail’;

// Run a for loop to check the available events
for Z := 0 to (AvailableList_Count) do
begin

event_code := StringReplace(json_response_available_events.Items[Z].ToString, ‘”‘, ”, [rfReplaceAll]);

if event_code_1.Contains(event_code) then
begin

if K < ListCount_Events then
begin

// Remove StyledSettings (FontColor)
lb_item.StyledSettings := lb_item.StyledSettings – [TStyledSetting.FontColor];

// Change TextSettings FontColor to available
lb_item.TextSettings.FontColor := $FF179ADF;

// Set the List CheckBox to checked
lb_item.IsChecked := true;
end;

end;
end;

end;

finally
begin
// Call to List start at position 0
lb_notifications.ItemIndex := 0;
end;
end;

end;
end;

I think my code smells and I want to know why and learn to do better.

Reasonable suggestions are welcome!

It takes too long to load the list on both Android and iOS.
When scrollable, it is incredibly slow on Android (but not on iOS).

How can I accelerate filling up this list?

Comments are closed.