Browse Tlist and load Tmemo with Tlist information

Asked

Viewed 492 times

0

[Tlist in firemonkey mobile]

Guys, I’ve had a problem here for a while.

I can’t work with TList in firemonkey mobile, specifically until then tested "Android". If I compile for Win32 works perfectly.

What I want to do is very simple. Create a list of pointers linked to product objects.

The error happens when I try to execute the procedure of loading the list inside the memo, something simple, I go through the list and I load the memo.

// -------------------------------------------------------------------------------------------
// Objeto responsável para armazenar informações dos produtos
// -------------------------------------------------------------------------------------------
TModelProd = class
private
FPRECO: Double;
FDESCRICAO: String;
FCODPROD: Integer;
procedure SetCODPROD(const Value: Integer);
procedure SetDESCRICAO(const Value: String);
procedure SetPRECO(const Value: Double);
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
property CODPROD: Integer read FCODPROD write SetCODPROD;
property DESCRICAO: String read FDESCRICAO write SetDESCRICAO;
property PRECO: Double read FPRECO write SetPRECO;
published
{ published declarations }
end;
// -------------------------------------------------------------------------------------------

Tenho FListProd: TList;
Quero criar o ModelProd na memória e armazenar na lista.
Exemplo: FListProd.Add(ModelProd);

Abaixo segue o código utilizado.

// -------------------------------------------------------------------------------------------
unit uFrmPrincipal;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Layouts, FMX.Memo, uModelProd;

type
TFrmPrincipal = class(TForm)
btnCriarLista: TButton;
Memo1: TMemo;
Panel1: TPanel;
btnCriarProd: TButton;
btnCarregarMemo: TButton;
procedure btnCriarListaClick(Sender: TObject);
procedure btnCriarProdClick(Sender: TObject);
procedure btnCarregarMemoClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FListProd: TList;
public
{ Public declarations }
end;

var
FrmPrincipal: TFrmPrincipal;

implementation

{$R *.fmx}

procedure TFrmPrincipal.btnCriarListaClick(Sender: TObject);
begin
if not Assigned(FListProd) then
FListProd := TList.Create;

btnCriarProd.Visible := True;
end;

procedure TFrmPrincipal.btnCriarProdClick(Sender: TObject);
Var
ModelProd: TModelProd;
iCodProd: Integer;
begin
// 1º ITEM
Inc(iCodProd);
ModelProd := TModelProd.Create;
ModelProd.CODPROD := iCodProd;
ModelProd.DESCRICAO := IntToStr(iCodProd) + '-TESTE';
ModelProd.PRECO := 20;
FListProd.Add(ModelProd);

// 2º ITEM
Inc(iCodProd);
ModelProd := TModelProd.Create;
ModelProd.CODPROD := iCodProd;
ModelProd.DESCRICAO := IntToStr(iCodProd) + '-TESTE';
ModelProd.PRECO := 20;
FListProd.Add(ModelProd);

// 3º ITEM
Inc(iCodProd);
ModelProd := TModelProd.Create;
ModelProd.CODPROD := iCodProd;
ModelProd.DESCRICAO := IntToStr(iCodProd) + '-TESTE';
ModelProd.PRECO := 20;
FListProd.Add(ModelProd);

// 4º ITEM
Inc(iCodProd);
ModelProd := TModelProd.Create;
ModelProd.CODPROD := iCodProd;
ModelProd.DESCRICAO := IntToStr(iCodProd) + '-TESTE';
ModelProd.PRECO := 20;
FListProd.Add(ModelProd);

// 5º ITEM
Inc(iCodProd);
ModelProd := TModelProd.Create;
ModelProd.CODPROD := iCodProd;
ModelProd.DESCRICAO := IntToStr(iCodProd) + '-TESTE';
ModelProd.PRECO := 20;
FListProd.Add(ModelProd);

btnCarregarMemo.Visible := True;
end;

procedure TFrmPrincipal.FormCreate(Sender: TObject);
begin
btnCriarLista.Visible := True;
btnCriarProd.Visible := False;
btnCarregarMemo.Visible := False;
end;

// O ERRO ACONTECE AQUI!!!
procedure TFrmPrincipal.btnCarregarMemoClick(Sender: TObject);
var
I: Integer;
begin
Memo1.Lines.Clear;
for I := 0 to FListProd.Count-1 do
Begin
Memo1.Lines.Add(TModelProd(FListProd.Items[i]).DESCRICAO);
End;
end;

end.
// -------------------------------------------------------------------------------

Link to source code: https://mega.co.nz#! vldxhI7a! Yl7_xgvjefb8z2kvtrktij3sdj3uurybpxmtavvsg0o I’d be very grateful if someone could help me. Thank you

  • 1

    What error occurs? What kind of exception is triggered?

1 answer

1

I’ve solved the mystery. It replaces the library System.Classes for System.Generics.Collation, changed the statement of TList.

Before it was:

FListaProd: TList

Now it’s:

FListaProd: TList<TModelProd>;

Now it’s working normally!

  • Leonam, you can accept your answer. Basque click on the icon below the total votes. Tip!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.