我想出了我自己的解決方案,使用TStringList和名稱/值對,這似乎工作得很好。我存儲在名爲條款標題,並在值所有的子欄目,然後我就分析了各個子項目
procedure LoadFromFile(AListView: TListView; AFileName: string);
var
I : Integer;
SL: TStringList;
Item: TListItem;
Col0, Col1, Col2, Col3, Col4, Col5: String;
begin
SL:= TStringList.Create;
try
SL.LoadFromFile(AFileName);
AListView.Items.BeginUpdate;
AListView.Items.Clear;
for I:= 0 to SL.Count - 1 do
begin
Item:= AlistView.Items.Add;
Item.Caption:= SL.Names[I];
parseValue(SL.ValueFromIndex[I], Col0, Col1, Col2, Col3, Col4, Col5);
Item.SubItems.Add(Col0);
Item.SubItems.Add(Col1);
Item.SubItems.Add(Col2);
Item.SubItems.Add(Col3);
Item.SubItems.Add(Col4);
Item.SubItems.Add(Col5);
end;
AListView.Items.EndUpdate;
finally
SL.Free;
end;
end;
procedure SaveToFile(AListView: TListView; AFileName: string);
var
I: Integer;
SL: TStringList;
begin
SL:= TStringList.Create;
for I := 0 to AListView.Items.Count - 1 do
begin
SL.Add(AlistView.Items[I].Caption + '=' +
AlistView.Items[I].SubItems[0] + ',' +
AlistView.Items[I].SubItems[1] + ',' +
AlistView.Items[I].SubItems[2] + ',' +
AlistView.Items[I].SubItems[3] + ',' +
AlistView.Items[I].SubItems[4] + ',' +
AlistView.Items[I].SubItems[5])
end;
try
SL.SaveToFile(AFileName);
finally
SL.Free;
end;
end;
procedure parseValue(AValue: String; var Col0, Col1, Col2, Col3, Col4, Col5: String);
var
I: Integer;
S: String;
L: TStringList;
begin
//create a temporary list to store the data parts in
L:= TStringList.Create;
try
//iterate through the length of the string
for I:= 1 to length(AValue) do
begin
//if the char is not a comma, append it to S
if AValue[I] <> ',' then
S:= S + AValue[I]
else
// if char is a comma, add S to temporary list and reset S
begin
L.Add(S);
S:= '';
end;
end;
//add the last string to temporary list
L.Add(S);
//assign the items in temporary list to variables to be passed back
Col0:= L[0];
Col1:= L[1];
Col2:= L[2];
Col3:= L[3];
Col4:= L[4];
Col5:= L[5];
finally
L.Free;
end;
end;
現在你達到了時間的價值時,你就會明白,[你應該保持數據從GUI控件分離](http://stackoverflow.com/questions/15057886/tlistview-column-sort-sort-by-first-two-columns#comment21171040_15057886)。 – TLama 2013-02-25 18:10:37
@TLama是對的,你真的不想讓視覺控制成爲你的主要數據結構。 – 2013-02-25 18:32:26
這只是一個簡單的destop應用程序,它不會持有多於幾百項 – JakeSays 2013-02-25 21:07:22