This entry is specifically related to my mobile news client, iNews, which can compile for iOS and Android, however it could be applied to any platform that FireMonkey supports, e.g. Windows and OSX.
When creating a mechanism for subscribing to groups available on a news server, I needed to figure out a way of the user being able to check or uncheck groups that they wanted to subscribe/unsubscribe to. Enter the EditMode property of TListView. When EditMode is set to True, a “checkbox” becomes visible in front of each item.
Because EditMode is being set to True after the items have been loaded, I figured I’d need to iterate the items and set the Checked property of each item depending on whether a flag was set in the database for that group. I thought no problem, I’ll just set ItemIndex to the loop variable and the dataset will be pointing to the corresponding record, right? Wrong; that only happens if a user actually clicks the item; probably a good thing. So I looked for a way of “faking” the item being clicked, without success. Then I looked for what the code does when a user actually clicks the item. Eureka! TLinkObservers.SelectionChanged causes the dataset to point to the correct record. Sadly, it became clear that doing this when iterating over all items is a performance killer, especially on the device.
Fortunately with the help of Jim Tierney, I discovered I was looking at this from the wrong way. With LiveBindings, you can create a link between the dataset and the Synch binding of the TListView. This causes the TListView to be populated with the records in the dataset, and synch the record pointer with the item in the TListView without having to write any code. When the item is being populated, LiveBindings calls the OnFilledListItem event on the link (which is a TLinkListToControlField link). There, I could check the flag and set the corresponding item’s Checked property, since the record pointer is at the record in question:
procedure TfrmMain.LinkListControlToField2FilledListItem(Sender: TObject; const AEditor: IBindListEditorItem); var LItem: TListViewItem; begin if not FGroupSubscribeUpdate and FSubscribeMode and (AEditor.CurrentIndex >= 0) then begin LItem := lvGroups.Items