delphi - 如何在 ListView 中显示文本文件中的数据?

标签 delphi listview

我想将数据从文本文件获取到 ListView 。

示例文本文件包含:

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

就像这样,我想获取 listview.items.caption 的第一个值和 sunitems[0] 的第二个值。 我想获取 ListView 行中的所有信息。

我怎样才能做到这一点?我使用了 stringlist.values,但我在所有行中获取了第 0 个成员数据。

最佳答案

将 TListView 拖放到窗体上,并将其样式设置为 vsList。创建您想要显示的三列(右键单击 ListView 并从弹出菜单中选择“列编辑器”)。

将以下内容添加到 FormShow() 事件(或任何您想要的位置):

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;

请注意,这假设您正在寻找的是这样的:

    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;

仅跳过子项中的空白行:

  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;

关于delphi - 如何在 ListView 中显示文本文件中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1668782/

相关文章:

java - 更改项目类型时 JavaFX 中的 ListView 出现 ClassCastException

android - 自定义日历 View 导致动画在创建时断断续续

c# - uwp 中的 ListView 组

Windows PaintDesktop版本

delphi - Win8设置系统还原点的方法

delphi - 帮助更好的复选框逻辑

delphi - 关于delphi引入进程信息的问题?

delphi - 无法读取 NetBIOS 状态

java - 在监听器外部更改 Android ListView 选定行属性

android - 最佳实践 : Update ArrayAdapter continuously