views:

440

answers:

1

I want to get data from a text file to a listview.

A sample text file contains:

0th member
first=XXXXXXXX
second=YYYYY000
1
first=XXXXXXX1
second=YYY1111
2
first=XXXXXX22
second=YYYY2222
3
first=XXXXXX33
second=YYYY333
4
first=XXXXX4444
second=YYY4444

Like that I want to get value of first to listview.items.caption and second to sunitems[0]. I want to get all information like that in listview lines.

How can I do that? I played with stringlist.values but I am getting 0th member data in all lines.

+5  A: 

Drop a TListView on a form, and set it's style to vsList. Create the three columns you'd like to display (right-click the ListView and choose Columns Editor from the popup menu).

Add the following to the FormShow() event (or wherever you'd like it):

procedure TForm1.FormShow(Sender: TObject);
var
  SL: TStringList;
  i: Integer;
begin
  SL := TStringList.Create;
  try
    SL.LoadFromFile(YourFileNameHere);
    i := 0;
    while i < SL.Count do
    begin
      with ListView1.Items.Add do
      begin
        Caption := SL[i];
        SubItems.Add(SL[i + 1]);
        SubItems.Add(SL[i + 2]);
      end;
      Inc(i, 3);
    end;
  finally
    SL.Free;
  end;
end;

Note that this assumes that what you're looking for is something like this:

    0th member          first=XXXXX          second=YYYYY
    1                   first=ZZZZZ          second=ZZZZZ

If what you're looking for is more like:

    0th member          XXXXX                YYYYY
    1                   ZZZZZ                ZZZZZ

Then change the SubItems() calls to something like this:

  SubItems.Add(Copy(SL[i + 1], Pos('=', SL[i + 1]) + 1, MaxInt);
  SubItems.Add(Copy(SL[i + 2], Pos('=', SL[i + 2]) + 1, MaxInt);

This extracts just the part after the equals (=) sign from the two subcolumn's text values.

That should be enough to get you started, I think.

Note that Delphi 2010 has a bug with the TListView when the ViewStyle is set to vsReport and you have defined no items in the IDE. You get a stream read error when you try and run your app because of the undefined items. You can work around this by creating a dummy item with a nonsense value at design time, and in your FormShow() event add the following as the first executable line:

    ListView1.Items.Clear;

This gets past the point that the DFM is streamed in, which is what triggers the bug.

EDIT: After reading comments by OP. To skip blank lines:

  // To skip entire group if starting line is blank
  while i < SL.Count - 1 do
  begin
    if SL[i] <> '' then
    begin
      with ListView1.Items.Add do
        // just like before
    end
    Inc(i, 3);
  end;

To skip only blank lines in subitems:

  while i < SL.Count - 1 do
  begin
    with ListView1.Items.Add do
    begin
      Caption := SL[i];
      if SL[i + 1] <> '' then
        SubItems.Add(SL[i + 1]);
      if SL[i + 2] <> '' then
        SubItems.Add(SL[i + 2];
    end;
    Inc(i, 3);
  end;
Ken White
thanks for reply sir , I need exactly like line 1 caption :XXXXXXXX subitem YYYYY000 line 2 caption : XXXXXX11 subitem :YYYY11111 ; line 3 -----(etc), and i have tried with u r method sir , and its working fine if all data line by line . in my text file ,all data doesnt line by line some times i use enter then i am getting empty fields in listview . is there any chance that i can get data regardless of how many emptly lines present in file ? thanks in advance .
noob
hmm , I got it to work sir ,with empty lines ,anyway thanks for u r idea sir . i am curious to know does i can do this with str.values ?
noob
For blanks, you can check first before adding (see my edit). I'm not sure what you mean by "with str.values"?
Ken White