我随便整了一下,发现其实也不难,贴一下大概代码吧,算是问题圆满结束拉。
代码没加注释,共两个单元,第一个是处理方式类,第二个是数据管理类,管理类中实现了两个处理,添加记录时按序号使用不同的处理方式类,显示切换时用RegListView(序号)。
type
PInfoData=^TInfoData;
TInfoData=record
SID:string;
Caption:string;
end;
TChangeEvent=procedure (Sender: TObject;AChanged:Boolean) of object;
TListViewInfo=class
private
FInfoHash:TSafeHashList;
FList:TList;
FFirst
ointer;
FLast
ointer;
FChangeEvent:TChangeEvent;
function GetCount: Integer;
function GetItem(Idx: Integer): PInfoData;
public
constructor Create(OnCustChange:TChangeEvent);
destructor Destroy;overload;
procedure Add(Key:string;Data:TInfoData);
property DataCount:Integer read GetCount;
property ItemByIdx[Idx:Integer]
InfoData read GetItem;
property OnChange:TChangeEvent read FChangeEvent write FChangeEvent;
end;
implementation
{ TListViewInfo }
procedure TListViewInfo.Add(Key: string; Data: TInfoData);
var Ptr
InfoData;
OldIdx:Integer;
begin
Ptr:=FInfoHash.KeyOf(Key);
if Ptr=nil then
begin
New(Ptr);
Ptr^:=Data;
FInfoHash.Add(Key,Ptr);
FList.Insert(0,Ptr);
if Assigned(FChangeEvent) then FChangeEvent(Self,True);
end else
begin
OldIdx:=FList.IndexOf(Ptr);
if OldIdx>=0 then
Ptr^:=Data;
FList.Move(OldIdx,0);
if Assigned(FChangeEvent) then FChangeEvent(Self,False);
end;
end;
constructor TListViewInfo.Create(OnCustChange:TChangeEvent);
begin
FInfoHash:=TSafeHashList.Create(997);
FList:=TList.Create;
OnChange:=OnCustChange;
FFirst:=nil;
FLast:=nil;
end;
destructor TListViewInfo.Destroy;
var
Idx: Integer;
Data
InfoData;
begin
for Idx := FList.Count - 1 downto 0 do
begin
Data:=PInfoData(FList.Items[Idx]);
Dispose(Data);
FList.Delete(Idx);
end;
FInfoHash.Free;
FList.Free;
inherited;
end;
function TListViewInfo.GetCount: Integer;
begin
Result:=FList.Count;
end;
function TListViewInfo.GetItem(Idx: Integer): PInfoData;
begin
Result:=nil;
if (Idx>=0)and(Idx<DataCount) then
Result:=FList.Items[Idx];
end;
type
TDataMgr=class
private
FList:TList;
FCurrListView:TListViewInfo;
FListView:TListView;
FHisPlay:Boolean;
function GetListViewData: TListViewInfo;
protected
procedure OnCustData(Sender: TObject; Item: TListItem);
procedure OnCustChange(Sender: TObject;AChanged:Boolean);
public
constructor Create(AListView:TListView);
destructor Destroy; override;
procedure RegListView(Idx:Integer);
procedure DoRefresh;
property ListViewData:TListViewInfo read GetListViewData;
end;
implementation
{ TDataMgr }
constructor TDataMgr.Create(AListView:TListView);
var ListView:TListViewInfo;
I:Integer;
begin
FList:=TList.Create;
for I:=0 to 1 do
begin
ListView:=TListViewInfo.Create(OnCustChange);
FList.Add(ListView);
end;
FCurrListView:=FList.Items[0];
FListView:=AListView;
FListView.DoubleBuffered:=True;
FListView.OwnerData:=True;
FListView.OnData:=OnCustData;
end;
destructor TDataMgr.Destroy;
begin
FList.Free;
inherited;
end;
procedure TDataMgr.DoRefresh;
begin
FListView.Items.Count:=ListViewData.DataCount;
FListView.Refresh;
end;
function TDataMgr.GetListViewData: TListViewInfo;
begin
Result:=FCurrListView;
end;
procedure TDataMgr.OnCustChange(Sender: TObject;AChanged:Boolean);
begin
if FCurrListView=Sender then
begin
if AChanged then
FListView.Items.Count:=FCurrListView.DataCount;//增加或减少了
end;
end;
procedure TDataMgr.OnCustData(Sender: TObject; Item: TListItem);
var ItemData
ListViewInfoData;
begin
ItemData:=PListViewInfoData(ListViewData.ItemByIdx[Item.Index]);
if ItemData<>nil then
with Item do begin
Caption:=ItemData^.SID;
SubItems.Add(ItemData^.Caption);
end;
end;
procedure TDataMgr.RegListView(Idx:Integer);
begin
if (Idx>=0)and(Idx<FList.Count) then
begin
FCurrListView:= FList.Items[Idx];
DoRefresh;
end;
end;