【发布时间】:2016-04-12 14:22:28
【问题描述】:
问题
我有 2 个不同的 json arrays,看起来像这样:
1 - 事件列表
{"0000:第一个事件名称","0001:第二个事件名称","0002:第三个事件名称"}
2 - 可用事件列表
{"0001","0002"}
然后我需要使用复选框生成一个包含可用事件的 ListBox:
- [ ] 第一个事件名称
- [x] 第二个事件名称
- [x] 第三个事件名称
给定解决方案
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;
我认为我的代码有异味,我想知道原因并学习做得更好。
欢迎提出合理的建议!
在
Android和iOS上加载列表需要太长。当可滚动时,
Android上的速度非常慢(但iOS上却没有)。
如何加快填写此列表的速度?
【问题讨论】:
-
添加许多项目时,在列表框上使用 beginupdate / endupdate。否则,它将对每个新项目进行(缓慢)重绘。
-
BeginUpdate/EndUpdate是一个很好的加载速度,但在 Android 上滚动仍然很慢。 -
FireMonkey TListBox 的滚动速度很慢,因为它的设计是每个 TListBoxItem 都是控件的容器。如果您希望在移动设备上滚动时获得更快的性能,请查看 TListView 或第 3 方 TListBox。
标签: android loops delphi listbox firemonkey