Einzelnen Beitrag anzeigen

Benutzerbild von cookie22
cookie22

Registriert seit: 28. Jun 2006
Ort: Düsseldorf
936 Beiträge
 
Delphi XE2 Professional
 
#8

Re: Virtual-ListView Code gesucht

  Alt 20. Jan 2010, 07:24
Zitat von Guido Eisenbeis:
Zitat von cookie22:
warum in onshow und nicht in onactivate?
Welchen Vorteil hätte das, bezogen auf den oben geschriebenen Zweck?

Dann noch eine Frage zu deinem obigen Tipp mit TCollection und TCollectionItem: Wie bist du denn auf diese Lösung gekommen? Ich hab mit TStringList versucht, die Items des ListViews zu handhaben. Das war wohl nicht der richtige Ansatz, denn Probleme gabs da einige. Zum Beispiel bei einer Sort-Funktion hatte ich noch keine Vorstellung, wie ich dann die Strings für die SubItems hanhaben sollte. Bei TCollectionItem übernimmt die Collection die Arbeit (glaube ich).

Zwei weitere Routinen seien des Dankes wegen auch erwähnt: TVirtualData.Update und TVirtualData.OnDataFind. Dass die im Code-Schnipsel drin sind, freut mich ebenfalls. Denn ich denke, um darauf zu kommen hätte ich einige Zeit gebraucht. Hast du den Code selbst entwickelt? Und hast du noch mehr davon? *unersättlich-ich-bin*

Guido.
ka, hatte das in meiner code schnipsel sammlung, woher das kommt weiss ich net mehr.

zum thema sortieren kann ich dir folgendes anbieten:
Delphi-Quellcode:
unit SortCollections;

interface

uses classes;

type
  TSortableCollection = class(TCollection)
  protected
    function Compare(Item1, Item2 : TCollectionItem) : integer; virtual;
    procedure QuickSort(L, R: Integer);
  public
    procedure Sort;
  end;

implementation

type
  // Helper class to allow sorting of a TCollection
  {$HINTS OFF}
  TShadowedCollection = class(TPersistent)
  private
    FItemClass: TCollectionItemClass;
    FItems: TList;
  end;
  {$HINTS ON}


{ TSortableCollection }

function TSortableCollection.Compare(Item1, Item2: TCollectionItem): integer;
begin
(*

Descendant classes would override this method and cast Item1 and Item2 to the
decendant class's collection item type perform the field comparisions

if item1.MyField < item2.MyField
  return -1
else if item1.MyField > item2.MyField
  return 1
else return 0

*)

  result := 0;
end;

procedure TSortableCollection.QuickSort(L, R: Integer);
var
  I, J, p: Integer;
  Save: TCollectionItem;
  SortList: TList;
begin
  //This cast allows us to get at the private elements in the base class
  SortList := TShadowedCollection(Self).FItems;

  repeat
    I := L;
    J := R;
    P := (L + R) shr 1;
    repeat
      while Compare(Items[I], Items[P]) < 0 do
        Inc(I);
      while Compare(Items[J], Items[P]) > 0 do
        Dec(J);
      if I <= J then begin
        Save := SortList.Items[I];
        SortList.Items[I] := SortList.Items[J];
        SortList.Items[J] := Save;
        if P = I then
          P := J
        else if P = J then
          P := I;
        Inc(I);
        Dec(J);
      end;
    until I > J;
    if L < J then
      QuickSort(L, J);
    L := I;
  until I >= R;
end;

procedure TSortableCollection.Sort;
begin
  if Count > 1 then
    QuickSort(0, pred(Count));
end;

end.
gruß,
cookie
  Mit Zitat antworten Zitat