ListView in Datei speichern
Wieso funktionieren die folgenden die folgenden Prozeduren nicht?
Ich hab mit den ListItems aus dem ListView gearbeitet
Delphi-Quellcode:
procedure LoadFromFile(ListView: TListView; const FileName: String);
var
Stream: TFileStream;
ListItem: TListItem;
ItemsCount: Integer;
SubItemsCount: Integer;
Str: String;
i, j: Integer;
begin
Stream := TFileStream.Create(FileName, fmOpenRead);
with ListView do
begin
// Items.Clear;
Stream.Read(ItemsCount, SizeOf(ItemsCount));
for i := 0 to ItemsCount - 1 do
begin
ListItem := Items.Add;
Stream.Read(Str, SizeOf(Str));
ListItem.Caption := Str;
Stream.Read(SubItemsCount, SizeOf(SubItemsCount));
for j := 0 to SubItemsCount - 1 do
begin
Stream.Read(Str, SizeOf(Str));
ListItem.SubItems.Add(Str);
end;
end;
end;
// Stream.Free;
end;
Delphi-Quellcode:
procedure SaveToFile(ListView: TListView; const FileName: String);
var
Stream: TFileStream;
ItemsCount: Integer;
SubItemsCount: Integer;
Str: String;
i, j: Integer;
begin
Stream := TFileStream.Create(FileName, fmCreate);
with ListView do
begin
ItemsCount := Items.Count;
Stream.Write(ItemsCount, SizeOf(ItemsCount));
for i := 0 to ItemsCount - 1 do
begin
Stream.Write(Items[i].Caption, SizeOf(Items[i].Caption));
SubItemsCount := Items[i].SubItems.Count;
Stream.Write(SubItemsCount, SizeOf(SubItemsCount));
for j := 0 to SubItemsCount - 1 do
begin
Str := Items[i].SubItems[j];
Stream.Write(Str, SizeOf(Str));
end;
end;
end;
end;