在GroupView处于活动状态时,向TListView添加或插入项目时始终将其添加到末尾

时间:2009-07-02 21:58:20

标签: delphi delphi-2009

在Delphi 2009中:

当TListView的GroupView处于活动状态时,向TListView添加或插入项目始终会将其添加到列表的末尾,而不管指定为param的Index。当GroupView设置为false时,它会将其添加到指定的索引处。但如果确实如此,则不会出现这种行为。

ListView2.Items.Insert(1)

以上内容应在指定的索引“1”处插入项目,但始终将其添加到列表的末尾。我在这里做错了什么?

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

和代码添加项目@ index 0

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

谢谢&amp;问候, 帕。

4 个答案:

答案 0 :(得分:1)

它可能取决于您更改的其他属性(如SortType) 我尝试使用一个简单的文本列表(使用ViewStyle = vsList)并在指定的索引处插入,是否设置了GroupView:

  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;

答案 1 :(得分:0)

您可能需要将新的ListItem分配给GroupIndex,假设您先添加了至少1个Group并为其指定了ID。

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

或者您可以先创建TListItem对象,为其指定GroupID并使用ListView.Items.AddItem(item,index)将其添加到ListView。

答案 2 :(得分:0)

我在Delphi XE中遇到了这个问题。无论Delphi如何包装COM控件或COM控件本身,它似乎都是非常基础的。奇怪的是,在“项目”列表中,项目的顺序正确,它们只是显示不正确。

为了解决这个问题,我最终删除了TListView中的所有项目,然后重新添加它们。

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;

这不是最好的解决方案,但它似乎有用(此代码是用Delphi XE编写的,但应该在Delphi 2009 +中工作)。

答案 3 :(得分:0)

BeginUpdate / EndUpdate帮助避免这种情况。 这段代码应该可以正常工作

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;