如何在 Delphi 中为动态外观模式下的ListView分配页脚?

mefy6pfw  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(142)

如何在FMX ListView处于动态外观模式时添加页脚文本?我一直收到异常20或80。我希望这不是一个错误。


请看下面的代码。

ListView1.Items.Clear; 
ListView1.BeginUpdate;
while not FDQuery1.Eof do
begin
  Item:=ListView1.Items.Add();
  Item.Tag:=FDQuery1.FieldByName('CustomerID').AsInteger;

  var LName    : TListItemText  := TListItemText(item.Objects.FindDrawable('Name'));
  LName.Font.Size:=12;
  var LCode    : TListItemText  := TListItemText(item.Objects.FindDrawable('Code'));

  var LImage1  : TListItemImage := TListItemImage(item.Objects.FindDrawable('Image1'));

  LImage1.ImageIndex := 0;

  LName.Text    := FDQuery1.FieldByName('Name').AsString;
  LCode.Text    := FDQuery1.FieldByName('Code').AsString;

    try
            Item := ListView1.Items.Add;
            Item.Text := 'TEST';
            Item.Purpose := TListItemPurpose.Footer;
         except
            on E : Exception do
               ShowMessage(E.ClassName+' '+E.Message);
         end;       
  FDQuery1.Next;
end;

FDQuery1.Close;
FDConnection1.Close;
ListView1.EndUpdate;
cqoc49vn

cqoc49vn1#

我无法重现(10.4.2或11.2)

procedure TForm33.Button1Click(Sender: TObject);
var group : String;
begin
ListView1.BeginUpdate;
ListView1.Items.Clear;
FDQuery1.Open();

group:=Copy(FDQuery1.FieldByName('CODE_APPOSE').asString,1,1);
while not FDQuery1.Eof do
begin
  if (group<>Copy(FDQuery1.FieldByName('CODE_APPOSE').asString,1,1)) then
  begin
   with ListView1.Items.Add do
    begin
      purpose:=TListItemPurpose.Footer;
      text:='End Group '+Group;
    end;
   group:=Copy(FDQuery1.FieldByName('CODE_APPOSE').asString,1,1);
   end;

  with ListView1.Items.Add do
    begin
     TListItemText(Objects.FindDrawable('Code')).Text:=FDQuery1.FieldByName('CODE_APPOSE').asString;
     TListItemText(Objects.FindDrawable('Description')).Text:=FDQuery1.FieldByName('LIBELLE_APPOSE').asString;
     TListItemImage(Objects.FindDrawable('Image')).ImageIndex:=FDQuery1.RecNo-1;
    end;

  FDQuery1.Next;
end;
   with ListView1.Items.Add do
    begin
      purpose:=TListItemPurpose.Footer;
      text:='End Group '+Group;
    end;

FDQuery1.Close;
ListView1.EndUpdate;
end;
s6fujrry

s6fujrry2#

毕竟,我已经发现,如果我们不检查对象,DynamicApparance模式不适合OnUpdateObject事件。最后,我可以设置页脚和页眉文本,同时更改字体大小和文本对象。请参见下面的代码。

procedure TFrmMain.GetCustomerList;
var
  Item : TListViewItem;
begin
      FDConnection1.Open;
      FDQuery1.Connection := FDConnection1;

      FDQuery1.SQL.Text := 'SELECT * FROM Customers';
      FDQuery1.Open;
      ListView1.Items.Clear;

      ListView1.BeginUpdate;

      while not FDQuery1.Eof do
      begin
           Item:=ListView1.Items.Add();
           Item.Tag:=FDQuery1.FieldByName('CustomerID').AsInteger;

           var LName    : TListItemText  := TListItemText(item.Objects.FindDrawable('Name'))   ; LName.Font.Size:=12;
           var LCode    : TListItemText  := TListItemText(item.Objects.FindDrawable('Code'))   ;
           var LAddress : TListItemText  := TListItemText(item.Objects.FindDrawable('Address')); LAddress.Font.Size:=11;
           var LCurrency: TListItemText  := TListItemText(item.Objects.FindDrawable('Currency'));
           var LImage1  : TListItemImage := TListItemImage(item.Objects.FindDrawable('Image1'));

           LImage1.ImageIndex := 0; 
           LName.Text    := FDQuery1.FieldByName('Name').AsString;
           LCode.Text    := FDQuery1.FieldByName('Code').AsString;
           LAddress.Text := FDQuery1.FieldByName('Address1').AsString.Trim; 

           with ListView1.Items.Add do
           begin
                purpose:=TListItemPurpose.Footer;
                text := 'TEST';
           end;
           FDQuery1.Next;
     end;
     FDQuery1.Close;
     FDConnection1.Close;
     ListView1.EndUpdate;
end;

procedure TFrmMain.ListView1UpdateObjects(const Sender: TObject;
const AItem: TListViewItem);
var
   LName     : TListItemText ;
   LCode     : TListItemText ;
   LAddress  : TListItemText ;
   LImage1   : TListItemImage;
begin

   if AItem.Objects.FindDrawable('Name') <> nil then
   begin
        LName     := TListItemText(AItem.Objects.FindDrawable('Name'));
        LCode     := TListItemText(AItem.Objects.FindDrawable('Code'));
        LAddress  := TListItemText(AItem.Objects.FindDrawable('Address'));
        LImage1   := TListItemImage(AItem.Objects.FindDrawable('Image1')) ;

        LName.Font.Size    := 13;
        LName.Width        := TListView(Sender).Width - LImage1.Width - 20;
        LAddress.Font.Size := 12;
        LAddress.Width     := TListView(Sender).Width - LImage1.Width - 20;
   end;
end;

相关问题