FMX TListView.Sort在delphi 10.3.3中不起作用

FMX TListView.Sort在delphi 10.3.3中不起作用,delphi,Delphi,我试图按字母顺序对TListView排序,但我使用的方法似乎不起作用 以下是通话代码: procedure TfMain.ButtonClick(Sender: TObject); begin miSort.IsChecked := not miSort.IsChecked; // miSort is a TMenuItem in a TPopupMenu if miSort.IsChecked then begin lvLinks.BeginUpdate; lvLinks.S

我试图按字母顺序对TListView排序,但我使用的方法似乎不起作用

以下是通话代码:

procedure TfMain.ButtonClick(Sender: TObject);
begin
miSort.IsChecked := not miSort.IsChecked; // miSort is a TMenuItem in a TPopupMenu
if miSort.IsChecked then
   begin
   lvLinks.BeginUpdate;
   lvLinks.Sort(AlphaSort);
   lvLinks.EndUpdate;
   end;
end;
排序方法需要TFmxObjectSortCompare

TFmxObjectSortCompare = reference to function (Left, Right: TFMXObject): Integer;
,这是我的:

function AlphaSort(Left, Right: TFMXObject): Integer;
begin
result := CompareText(TListViewItem(Left).Text, TListViewItem(Right).Text) // require System.SysUtils
end;

无论是否使用BeginUpdate…EndUpdate,它都不起作用。

在FMX
TListView
中继承自
TAdapterListView
,它公开了
IListViewAdapter
类型的
适配器属性。此界面为列表项提供了一种排序方法

根据您的示例,这种方法可能是有效的:

  lvLinks.Adapter.Sort(TDelegatedComparer<TListItem>.Create(
    function(const Left, Right: TListItem): Integer
    begin
      Result := CompareText(TListViewItem(Left).Text, TListViewItem(Right).Text);
    end));
lvLinks.Adapter.Sort(TDelegatedComparer.Create(
函数(常量左、右:TListItem):整数
开始
结果:=CompareText(TListViewItem(左)。Text,TListViewItem(右)。Text);
(完),;

与问题无关,但仍然:您需要使用
try..finally
块保护
BeginUpdate..EndUpdate
。否则,如果在
Sort
中引发异常,您将陷入
BeginUpdate
状态,这是非常糟糕的。始终这样做:
lvLinks.BeginUpdate;最后尝试{do stuff}lvLinks.EndUpdate;结束
(当然要有适当的空格和缩进)。