Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TListView groups and items do not appear after clearing and reloading groups and items

Delphi XE4 Update 1 and Windows 8.

When I add groups and items to the list view, they are displayed properly. When I then clear the items and groups, and add them again, nothing appears. Surely this is not the intended behavior?

From the DFM:

object lv: TListView
  Left = 24
  Top = 20
  Width = 250
  Height = 225
  Columns = <
    item
      Caption = 'Model'
      Width = 180
    end>
  GroupView = True
  ReadOnly = True
  RowSelect = True
  TabOrder = 0
  ViewStyle = vsReport
end

The code:

procedure TForm1.Button1Click(Sender: TObject);
var
  LListGroup: TListGroup;
  LListItem: TListItem;
begin
  lv.Items.Clear;
  lv.Groups.Clear;

  LListGroup := lv.Groups.Add;
  LListGroup.Header := 'Ford';

  LListItem := lv.Items.Add;
  LListItem.Caption := 'Escape';
  LListItem.GroupID := LListGroup.ID;

  LListItem := lv.Items.Add;
  LListItem.Caption := 'F150';
  LListItem.GroupID := LListGroup.ID;

  OutputDebugString(PChar(Format('lv.Groups.Count=%d', [lv.Groups.Count])));
end;

The first time I click the button, the items appear and they're grouped. The second time, the list view is blank. If I comment out the line that clears the groups, then it works, but the number of groups, all of which are unused but one, grows by 1 each time.

like image 846
Pat Avatar asked Mar 12 '14 17:03

Pat


1 Answers

The issue with your code is which you are passing the ID property of the TCollectionItem to the GroupID property of the TListItem and you must use the GroupID property of the TListGroup.

So change this line

  LListItem.GroupID := LListGroup.ID; //here you are passing a wrong id for the group

to

  LListItem.GroupID := LListGroup.GroupID; //This is a valid assignment for the GroupID property
like image 89
RRUZ Avatar answered Sep 19 '22 19:09

RRUZ