Search code examples
delphifiremonkey

What happened to Delphi's ListView in FMX (.Detail and .Text are missing)?


Some years ago, I used to use code like this:

ShowMessage(lvDrinksListview.Items.Item[lvDrinksListview.ItemIndex].Text);
ShowMessage(lvDrinksListview.Items.Item[lvDrinksListview.ItemIndex].Detail);

I am now using Delphi 11 Community Edition, and this code reports errors:

[dcc64 Error] uMain.pas(652): E2003 Undeclared identifier: 'Detail'
[dcc64 Error] uMain.pas(651): E2003 Undeclared identifier: 'Text'

This is in an FMX app.

What is the replacement for Detail and Text?


Solution

  • There was a big refactoring of TListView in Delphi 10.0 Seattle. Your old code predates that release.

    The Item[] property now returns a base TListItem class, which does not have the Detail or Text properties that you are looking for, hence the errors.

    To resolve this, you can simply type-cast each Item to TListViewItem, which derives from TListItem and does have the properties you want, eg:

    ShowMessage(TListViewItem(lvDrinksListview.Items[lvDrinksListview.ItemIndex]).Text);
    ShowMessage(TListItemItem(lvDrinksListview.Items[lvDrinksListview.ItemIndex]).Detail);
    

    I would suggest you use a local variable to clean that up a bit:

    var Item := TListViewItem(lvDrinksListview.Items[lvDrinksListview.ItemIndex]);
    ShowMessage(Item.Text);
    ShowMessage(Item.Detail);