ListView, do not repeat records

2

I have 2 listview and a timer. This timer captures the records that exist with a particular comment and puts it in the other listview. It turns out that it is repeating the records, I would like it to add only once the record, and check, if already exists the record in the other LISTVIEW, it ignores. What am I doing wrong ? Here is my code:

 for I := 0 to Form1.LV1.Items.Count-1 do
      begin
      if Form1.LV1.Items[I].SubItems[5] = 'OK' then // confere se existe o comentário
      begin
      Form1.LV2.Visible := True;
      L := Form1.LV2.Items.Add;
      for J := 0 to Form1.LV2.Items.Count-1 do
      if Form1.LV2.Items[J].SubItems[5] <> Form1.LV1.Items[I].SubItems[5] then  // aqui tentei fazer não repetir (não deu certo).

      Form1.LV2.Items.item[Form1.LV2.Items.count-1] := Form1.LV1.items.item[i];
      end;

Any ideas?

    
asked by anonymous 05.06.2014 / 18:27

1 answer

2

Since I did not know which one you wanted, I did both. With regard to logic, with the empty listview you always add, after all, no items, no repetitions and from the second apply the function you prefer

function VerificaLVSubItem(Lista: TListView;Subitem:integer; Verifica:String):string;
var i : integer;
begin
  Result := '1';
  for i := 0 to lista.Items.Count - 1 do
  begin
    if lista.Items[i].SubItems[subitem] = Verifica then
      Result := '';
  end;
end;

function VerificaLVItem(Lista: TListView;Verifica:String):string;
var i : integer;
begin
  Result := '1';
  for i := 0 to lista.Items.Count - 1 do
  begin
    if lista.Items[i].Caption = Verifica then
      Result := '';
  end;
end;

And in the call you put:

  if lv2.Items.Count > 0 then
  begin
    if VerificaLVSubItem(LV2,1,'1') = '' then
      Showmessage('Tem')
    else
      Showmessage('Não Tem');
    if VerificaLVItem(LV2,'1') = '' then
      Showmessage('Tem')
    else
      Showmessage('Não Tem');
  end
  else
    Showmessage('Adiciona');
    
05.06.2014 / 18:57