Adding or inserting an item to a TListView always adds it to the end when GroupView is Active

自古美人都是妖i 提交于 2019-12-23 13:07:01

问题


In Delphi 2009 :

When TListView's GroupView is Active, adding or inserting an item to a TListView always adds it to the end of the list, regardless of Index specified as param. When GroupView is set to false it adds it at the specified index. But when it is true, this behavior is not seen.

ListView2.Items.Insert(1)

The above should insert item at the sepecified index "1", but always adds it to the end of the list. What am I doing wrong here?

object ListView2: TListView
Left = 32
Top = 40
Width = 161
Height = 233
BorderWidth = 5
Columns = <
  item
    AutoSize = True
  end>
DoubleBuffered = False
FlatScrollBars = True
Groups = <
  item
    Header = 'test'
    Footer = 'aksdlkajsd;flkj'
    GroupID = 0
    State = [lgsNormal]
    HeaderAlign = taLeftJustify
    FooterAlign = taLeftJustify
    Subtitle = 'adgasdfasdf'
    TopDescription = 'test desc'
    BottomDescription = 'adsfasdfasdf'
    TitleImage = 0
    ExtendedImage = 0
  end
  item
    Header = 'test1'
    GroupID = 1
    State = [lgsNormal]
    HeaderAlign = taLeftJustify
    FooterAlign = taLeftJustify
    TopDescription = 'test1 desc'
    TitleImage = 1
    ExtendedImage = 1
  end>
HideSelection = False
IconOptions.WrapText = False
Items.ItemData = {
  03D80000000500000000000000FFFFFFFFFFFFFFFF0000000000000000000000
  0003740077006F00FFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000000
  086100730064006600610073006400660000000000FFFFFFFFFFFFFFFF000000
  000000000000000000057400680072006500650000000000FFFFFFFFFFFFFFFF
  000000000000000000000000036F006E00650000000000FFFFFFFFFFFFFFFF00
  00000000000000000000001866006F0075007200320033003300330033003300
  33003300330033003300330033003300330033003300330033003300}
MultiSelect = True
GroupView = True
ParentDoubleBuffered = False
ShowColumnHeaders = False
TabOrder = 0
ViewStyle = vsReport

end

and Code to add item @ index 0

procedure TForm1.Button1Click(Sender: TObject);
var
  oListItem: TListItem;
begin
  oListItem := ListView2.Items.Insert(0);
  oListItem.Caption := 'CCCCCCCC';
  oListItem.GroupID := 0;
end;

Thanks & Regards, Pavan.


回答1:


It might depend on other properties you changed (like SortType).
I tried with a simple text list (with ViewStyle =vsList) and it inserts at the specified index wether GroupView is set or not:

  object ListView1: TListView
    Left = 24
    Top = 16
    Width = 250
    Height = 150
    Columns = <>
    Items.ItemData = {
      03480000000200000000000000FFFFFFFFFFFFFFFF00000000FFFFFFFF000000
      00057400650073007400310000000000FFFFFFFFFFFFFFFF00000000FFFFFFFF
      000000000574006500730074003200}
    GroupView = True
    TabOrder = 0
    ViewStyle = vsList
  end

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListView1.Items.Insert(1).Caption := Edit1.Text;
end;



回答2:


You may need to assign the new ListItem to a GroupIndex, assuming that you have added at least 1 Group first and given it an ID.

var 
  item:  TListItem;
begin
  item:= ListView.Items.Add;
  item.GroupID=0;
end;

Or you can create the TListItem object first, give it the GroupID and use ListView.Items.AddItem(item, index) to add it to the ListView.




回答3:


I ran into this problem in Delphi XE. It appears to be pretty fundamental in either how Delphi wrapped the COM control or the COM control itself. Oddly enough, in the Items list the items are in the correct order, they are just displayed incorrectly.

To fix this I ended up deleting all of the items out of the TListView and then adding them back in.

procedure RefreshListView(const ListView: TListView);
  var ListItem : TListItem;
      List : TList<TPair<String,Boolean>>;
      Pair : TPair<String,Boolean>;
begin
  List := TList<TPair<string,Boolean>>.Create;
  try
    ListView.Items.BeginUpdate;
    try
      //To get the sorting to work right in the listview with GridView and vsReport
      //You have to rebuild the list completely
      for ListItem in ListView.Items do
      begin
        List.Add(TPair<String,Boolean>.Create(ListItem.Caption,ListItem.Selected));
      end;

      ListView.Items.Clear;

      for Pair in List do
      begin
        with ListView.Items.Add do
        begin
          Caption := Pair.Key;
          Selected := Pair.Value;
        end;
      end;
    finally
      ListView.Items.EndUpdate;
    end;
  finally
    List.Free;
  end;
end;

This isn't the best solution, but it seems to work (this code was written in Delphi XE, but should work in Delphi 2009+).




回答4:


BeginUpdate/EndUpdate help to avoid this. This code should work fine

procedure TForm1.Button1Click(Sender: TObject);
var
  oListItem: TListItem;
begin
 ListView2.Items.BeginUpdate;
 oListItem := ListView2.Items.Insert(0);
 oListItem.Caption := 'CCCCCCCC';
 oListItem.GroupID := 0;
 ListView2.Items.EndUpdate;
end;


来源:https://stackoverflow.com/questions/1076861/adding-or-inserting-an-item-to-a-tlistview-always-adds-it-to-the-end-when-groupv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!