ARTHUR LEMES
ARTHUR LEMES

Reputation: 3

How to add Images to a TListView item in FMX?

I already connected the ListView to my ImageList, but I don't know what I am doing wrong.

Here is my code:

var
  Item: TListViewItem;
begin
  UniQuery1.SQL.Text := 'SELECT * FROM Clients WHERE Problem IS NOT NULL';
  UniQuery1.Open;
  ListView1.Items.Clear;
  if not UniQuery1.IsEmpty then
  begin
    ListView1.Images := ImageList1;
    while not UniQuery1.Eof do
    begin
      Item := ListView1.Items.Add;
      Item.Text := 'Name: ' + UniQuery1.FieldByName('Name').AsString;
      Item.Detail := 'Number: ' + UniQuery1.FieldByName('Number').AsString +
                     ' | Problem: ' + UniQuery1.FieldByName('Problem').AsString;

      Item.ImageIndex := 0;
      UniQuery1.Next;
    end;
  end;
  UniQuery1.Close;
end;

Upvotes: 0

Views: 62

Answers (2)

SergeGirard
SergeGirard

Reputation: 441

Still problems:

My short response, use the OnUpdateObjects event:

procedure TMain.ListView1UpdateObjects(const Sender: TObject;
  const AItem: TListViewItem);
begin
  AItem.ImageIndex:=0;
end;

Long response : I should suggest another approach, "no loop code" solution, involving LiveBindings.

Change your query (and optimize, this SELECT * is a "bad" habit):

SELECT number,name,problem,0 as index FROM Clients WHERE Problem IS NOT NULL

Then you can use field 'index' linked with Item.ImageIndex:

image

Ok, if you are not familiar with LiveBindings CustomFormat property, my response don't apply to your Text and Detail format. Easy way, change your SQL:

SELECT 'Name : '||name fname,
       'Number: '||Number||' | Problem: '||problem detail,
       0 as Index FROM Clients WHERE Problem IS NOT NULL

Just one inconvenient, the DataSet has to stay open.

Question : is the TListView component mandatory? A TListBox should be sufficient, if you draw an ItemStyle.

More info in my French blog or tutorials site.

Upvotes: 0

Uwe Raabe
Uwe Raabe

Reputation: 47809

The visual appearance of items in an FMX TListView is controlled by its ItemAppearance settings - in this case its similar named sub-property ItemAppearence, which defaults to ListItem. Changing this to ImageListItem should solve your problem.

Upvotes: 1

Related Questions