Entwickler-Ecke

Grafische Benutzeroberflächen (VCL & FireMonkey) - ListView füllen mit Werten aus StringList


Gerhard_S - Sa 08.09.12 02:25
Titel: ListView füllen mit Werten aus StringList
Ich bekomme von Indys FTP eine Stringliste, deren Zeilen so aussehen:
Type=file;Size=1297;Modify=20110610180717;Unique=0520002601ccd6e8;Perm=adfrw; browser1.php
Sie sollen in eine ListView geschrieben werden, die folgende Spalten hat:
Type, Size, Modified, ID, Permissions, Name
Wie verbinde ich das Aufspalten der Wertepaare (die letzte Spalte ist eine Ausnahme) mit dem Eintrag in die ListView?


mandras - Sa 08.09.12 13:02

Formular:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 870
  Height = 500
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 16
    Top = 80
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object ListView1: TListView
    Left = 144
    Top = 72
    Width = 425
    Height = 169
    Columns = <
      item
        Caption = 'Type'
      end
      item
        Caption = 'Size'
      end
      item
        Caption = 'Modified'
        Width = 100
      end
      item
        Caption = 'ID'
        Width = 70
      end
      item
        Caption = 'Permissions'
      end
      item
        Caption = 'Name'
        Width = 100
      end>
    TabOrder = 1
    ViewStyle = vsReport
  end
end

Code:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
const TheString='Type=file;Size=1297;Modify=20110610180717;Unique=0520002601ccd6e8;Perm=adfrw; browser1.php';
      Trenner = ';';

procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;
    SL:TStringList;
    ListItem: TListItem;

begin
 SL:=TStringList.Create;
 SL.Delimiter := Trenner;
 SL.CaseSensitive := false;
 i:=1;
 while i<=length(TheString) do begin
  j:=i;
  while (j<=length(TheString)) and (TheString[j] <> Trenner) do j:=j+1;
  SL.Add(copy(TheString,i,j-i));
  i:=j+1;
 end;
 ListItem:=ListView1.Items.Add;
 ListItem.Caption := SL.Values['Type'];
 ListItem.SubItems.Add(SL.Values['Size']);
 ListItem.SubItems.Add(SL.Values['modify']);
 ListItem.SubItems.Add(SL.Values['unique']);
 ListItem.SubItems.Add(SL.Values['Perm']);
 ListItem.SubItems.Add(SL[SL.Count-1]);
 SL.Free;
end;


Gerhard_S - So 09.09.12 00:59

Danke für den Code. Jetzt ist es relativ einfach:
Um die vollständige StringListe (SL1) in die ListView zu schreiben, genügt eine einfache Schleife, z.B. so:

Delphi-Quelltext
1:
2:
3:
4:
5:
for h:= 0 to SL1.Count -1 do
   begin
       TheString := SL1[h];
       CheckIt(TheString, SL);
   end;

CheckIt kann dann so aussehen:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
procedure TForm1.CheckIt(TheString: string; SL : TStringList);
var i, j: integer;
    ListItem: TListItem;
    Astr, Trenner: string;
begin
    i := 1;
    Trenner := ';';
    while i<=length(TheString) do
         begin
          j:=i;
          while (j<=length(TheString)) and (TheString[j] <> Trenner)
             do Inc(j);
          AStr := copy(TheString,i,j-i);
          SL.Add(AStr);
          i:=j+1;
         end;
       ListItem:= ListView1.Items.Add;
       ListItem.Caption := SL.Values['Type'];
       ListItem.SubItems.Add(SL.Values['Size']);
       ListItem.SubItems.Add(SL.Values['modify']);
       ListItem.SubItems.Add(SL.Values['unique']);
       ListItem.SubItems.Add(SL.Values['Perm']);
       ListItem.SubItems.Add(SL[SL.Count-1]);
       SL.Clear;
end;