如何使用TAdapterBindSource

时间:2019-06-11 06:52:13

标签: delphi firemonkey

我正在使用Delphi 10.3.1(Firemonkey FMX)构建Android和iOS应用。我有一个TListView,与AdapterBindSource实时绑定。我的问题是:适配器刷新后,不会出现新记录

==============

  1. 我创建了一个TObjectList,向其中添加了3个对象
  2. 我通过传递一个TObjectList来创建TBindSourceAdapter。
  3. 我将TBindSourceAdapter分配给AdapterBindSource1.Adapter。
  4. 然后我释放TObjectList并重新创建它,添加4个新创建的对象(其中3个是旧记录,已修改了一些数据,1个是新记录)
  5. 我执行TBindSourceAdapter.Refresh和TAdapterBindSource.Refresh
  6. 这3条旧记录已成功刷新,并显示了修改后的数据,但新记录未在Android和iOS中显示
  7. 相同的逻辑在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条记录。

注释:

  1. 在刷新期间,我没有重新创建TListBindSourceAdapter并再次将其分配给AdapterBindSource1.Adapter,但记录仍成功刷新。
  2. 但是,即使我重新创建TListBindSourceAdapter并再次分配给AdapterBindSource1.Adapter,新记录仍然不会显示,只会导致内存泄漏。

我该如何解决?我缺少想刷新TListView的东西吗?还是我的BindSourceAdapter刷新逻辑错了?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我找到了刷新TListView的解决方案。

发生此问题是由于在此处重新创建了TObjectList:

The ansible playbook is as follows---
- hosts: all
  become: yes
  tasks:
  - name: Chenking ping
    ping:
  - name: Update packages
    apt:
      name: apache2
      update_cache: yes
      state: present
  - name: restart apache2 server
    service:
      name: apache2
      enable: yes
      state: restarted
  - name: install php module
    apt:
      name: "{{ item }}"
      state: present
    with_items:
     - php
     - libapache2-mod-php5
     - php-mcrypt
     - php-mysql
  - name: restart apache2 afetr restart
    service:
      name: apache2
      enable: yes
      state: restarted

释放列表并重新创建导致此问题。我更改了它以清除列表并向其中添加新对象。

FreeAndNil(FList);
FList := TDataList.Create; // re-create the list, then create Tdata object and add to it again.

然后AdapterBindSource成功刷新。

如果TObjectList释放并重新创建,则适配器将不再正确使用它。