如何使用 TAdapterBindSource 刷新 TListView LiveBinding
How to refresh TListView LiveBinding with TAdapterBindSource
我正在使用 Delphi 10.3.1 (Firemonkey FMX) 构建 android 和 iOS 应用程序。我有一个 TListView,与 AdapterBindSource 实时绑定。我的问题是:适配器刷新后没有出现新记录。
==============
- 我创建了一个 TObjectList,并向其中添加了 3 个对象
- 我通过传递一个 TObjectList 来创建一个 TBindSourceAdapter。
- 我将 TBindSourceAdapter 分配给 AdapterBindSource1.Adapter。
- 然后我释放TObjectList并重新创建它,添加4个新创建的对象(其中3个是旧记录,修改了一些数据,1个是新记录)
- 我做 TBindSourceAdapter.Refresh 和 TAdapterBindSource.Refresh
- 这 3 条旧记录已成功刷新并显示修改后的数据,但新记录未显示在 Android 和 iOS
- 相同的逻辑在Windows平台
中工作正常
==============
我的逻辑
创建 TObjectList
首先我从 Rest Server 获取记录并转换成 TObjectList
TData : class(TObject) ... // a class stored some data
TDataList = class(TObjectList<TData>)
// then I get data from Rest Server and created FList, it is a Form private variable
FList := TDataList.Create; // a private Form variable
// create Tdata objects and add to FList .....
创建 TBindSourceAdapter,分配给 AdapterBindSource
var ABindSourceAdapter: TBindSourceAdapter;
// ....
ABindSourceAdapter := TListBindSourceAdapter<TData>.Create(self, FList, True);
AdapterBindSource1.Adapter := ABindSourceAdapter;
AdapterBindSource1.Active := true;
然后在 ListView 上显示与 AdapterBindSource 实时绑定的记录
刷新 FList 记录
当点击刷新按钮时,我再次触发从 Rest 服务器获取数据,我释放了 FList 并重新创建它
FreeAndNil(FList);
FList := TDataList.Create; // re-create the list, then create Tdata object and add to it again.
刷新适配器
然后我刷新适配器
AdapterBindSource1.Adapter.Refresh;
AdapterBindSource1.Refresh;
这里3条旧记录刷新成功,修改后的数据正确显示,但是新记录不显示,TListView仍然只显示3条记录。
备注:
- 我在刷新时没有重新创建TListBindSourceAdapter并重新分配给AdapterBindSource1.Adapter,记录仍然刷新成功。
- 但是,即使我重新创建TListBindSourceAdapter并再次分配给AdapterBindSource1.Adapter,新记录仍然没有出现,只是导致内存泄漏。
我该如何解决这个问题?有没有我想刷新 TListView 的东西?还是我的BindSourceAdapter刷新逻辑有误?
感谢您的帮助。
我找到了刷新 TListView 的解决方案。
问题的发生是由于 re-create 的 TObjectList 在这里:
FreeAndNil(FList);
FList := TDataList.Create; // re-create the list, then create Tdata object and add to it again.
释放列表并且 re-create 导致了这个问题。我将其更改为清除列表并向其中添加新对象。
FList.Clear();
// then add objects to it again, such as FList.AddRange(...)
AdapterBindSource刷新成功
如果 TObjectList 可用且 re-created,适配器将不再正确使用它。
我正在使用 Delphi 10.3.1 (Firemonkey FMX) 构建 android 和 iOS 应用程序。我有一个 TListView,与 AdapterBindSource 实时绑定。我的问题是:适配器刷新后没有出现新记录。
==============
- 我创建了一个 TObjectList,并向其中添加了 3 个对象
- 我通过传递一个 TObjectList 来创建一个 TBindSourceAdapter。
- 我将 TBindSourceAdapter 分配给 AdapterBindSource1.Adapter。
- 然后我释放TObjectList并重新创建它,添加4个新创建的对象(其中3个是旧记录,修改了一些数据,1个是新记录)
- 我做 TBindSourceAdapter.Refresh 和 TAdapterBindSource.Refresh
- 这 3 条旧记录已成功刷新并显示修改后的数据,但新记录未显示在 Android 和 iOS
- 相同的逻辑在Windows平台 中工作正常
==============
我的逻辑
创建 TObjectList
首先我从 Rest Server 获取记录并转换成 TObjectList
TData : class(TObject) ... // a class stored some data
TDataList = class(TObjectList<TData>)
// then I get data from Rest Server and created FList, it is a Form private variable
FList := TDataList.Create; // a private Form variable
// create Tdata objects and add to FList .....
创建 TBindSourceAdapter,分配给 AdapterBindSource
var ABindSourceAdapter: TBindSourceAdapter;
// ....
ABindSourceAdapter := TListBindSourceAdapter<TData>.Create(self, FList, True);
AdapterBindSource1.Adapter := ABindSourceAdapter;
AdapterBindSource1.Active := true;
然后在 ListView 上显示与 AdapterBindSource 实时绑定的记录
刷新 FList 记录
当点击刷新按钮时,我再次触发从 Rest 服务器获取数据,我释放了 FList 并重新创建它
FreeAndNil(FList);
FList := TDataList.Create; // re-create the list, then create Tdata object and add to it again.
刷新适配器
然后我刷新适配器
AdapterBindSource1.Adapter.Refresh;
AdapterBindSource1.Refresh;
这里3条旧记录刷新成功,修改后的数据正确显示,但是新记录不显示,TListView仍然只显示3条记录。
备注:
- 我在刷新时没有重新创建TListBindSourceAdapter并重新分配给AdapterBindSource1.Adapter,记录仍然刷新成功。
- 但是,即使我重新创建TListBindSourceAdapter并再次分配给AdapterBindSource1.Adapter,新记录仍然没有出现,只是导致内存泄漏。
我该如何解决这个问题?有没有我想刷新 TListView 的东西?还是我的BindSourceAdapter刷新逻辑有误?
感谢您的帮助。
我找到了刷新 TListView 的解决方案。
问题的发生是由于 re-create 的 TObjectList 在这里:
FreeAndNil(FList);
FList := TDataList.Create; // re-create the list, then create Tdata object and add to it again.
释放列表并且 re-create 导致了这个问题。我将其更改为清除列表并向其中添加新对象。
FList.Clear();
// then add objects to it again, such as FList.AddRange(...)
AdapterBindSource刷新成功
如果 TObjectList 可用且 re-created,适配器将不再正确使用它。