我怎样才能加快填充这个 TListBox 的速度?

How can I speed up filling this TListBox?

问题

我有 2 个不同的 json arrays,看起来像这样:

1 - 事件列表

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

2 - 可用事件列表

{"0001","0002"}

然后我需要使用复选框生成一个包含可用事件的列表框:



给出的解决方案

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;

我觉得我的代码有味道,我想知道原因并学习做得更好。

欢迎提出合理建议!

我怎样才能加速填写这个列表?

按照@birger的建议,用简单的2行代码就解决了:

BeginUpdate;
//
GetEvents;
//
EndUpdate;

如果您使用 Delphi 组件,例如 ListBoxMemo、ListView...,并且您添加或修改了很多项目(线、节点、.. .), 组件的性能变得很慢。这是因为每次更改后,都会在屏幕上重新绘制。

BeginUpdateEndUpdate 在添加、删除或插入项目时防止过度重绘并加快处理时间。

参考:http://www.festra.com/eng/tip-beginupdate.htm