我如何在不使用所选项目的情况下获取其子项目等于某个字符串的列表视图项目的索引?

how do i get index of listview item where its subitem equal some string without using selected items?

我目前在我的项目中使用列表视图我想通过查找它的子项字符串来获取某些项目的索引,我有带有项目和子项的列表视图,我想找到 item caption := namesubitem := id该项目的索引 sub item := id,我该怎么做,我搜索了一些方程式,但还没有找到。我需要这个的原因是因为子项目 ID 具有唯一 ID 并且这非常安全,而不是使用按标题查找项目

您需要遍历列表视图的 Items,查看要匹配的正确子项。例如,给定一个包含三列(A、B 和 C)的 TListView,要搜索 B 列以查找内容:

function TForm1.FindListIndex(const TextToMatch: string): Integer;
var
  i: Integer;
begin
  for i := 0 to ListView1.Items.Count - 1 do
    if ListView1.Items[i].SubItems[1] = TextToMatch then
      Exit(i);
  Result := -1;
end;

当然,替换成你自己的匹配函数(比如SameText):

if SameText(ListView1.Items[i].SubItems[1], TextToMatch) then
   ...;

如果你想在任何子项中搜索匹配项,你只需要一个嵌套循环:

function TForm1.FindListIndex(const TextToMatch: string): Integer;
var
  i, j: Integer;
begin
  for i := 0 to ListView1.Items.Count - 1 do
    for j := 0 to ListView1.Items[i].SubItems.Count - 1 do
      if ListView1.Items[i].SubItems[j] = TextToMatch then
        Exit(i);
  Result := -1;
end;