Listview, do not repeat records

Asked

Viewed 542 times

2

I have 2 listview and a timer. This timer captures the records that exist with a certain comment and puts in the other listview. It turns out that he is repeating the records, I would like him to add only once the record, and checasse, if the record already exists in the other LISTVIEW, he ignores it. What am I doing wrong? Follow 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 idea ?

1 answer

2


I didn’t know which one you wanted, so I did both jobs. With respect 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 on 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');
  • it even worked in parts...what the problem ? He needs to add the record 1 time, and start doing these tests from the second, to see if there are repeats. Why ? Because if not already in the first record he will be talking that exists, understood ? For the first time I need to let it be added and then test if it is repeated. I don’t know if I understand.

  • Before adding you check, if there is no, on the Else you enter. Would it be so, first check, doesn’t it exist? Adds.

  • I tried that @Filipe.Fonseca, I don’t know why it doesn’t work, I don’t know if it has to do with LISTVIEW being blank, I say the second listview before adding the ITEM, see the code: http://pastebin.com/waNQhJqs

  • Quick question, @user7605 you are comparing items with subitens?

  • good question, actually I need to compare the 2 subitems, if it is equal, it does not add.

  • Then I’ll need to change the Findlistviewitem routine to check everything. As soon as I have a little time I’ll do it for you.

  • it’s true @Filipe.Fonseca, the function is comparing a subitem to an item, had not even connected me in this!! Thanks friend just need this to finish the project.

  • even could be compare only the items, do not need to compare the subitem. I just need to see if that record already exists or not in the other listview. What I need to change in this case ?

  • You want items or subitens?

  • subitems mesmo.... changes a lot of things ?

  • Well, I did both, Take a look at Edit, @user7605

  • I can use it like this? S := Form1.LV1.Items[i]. Subitems[3]; L4 := Verificalvsubitem(Form1.LV2, 3, S);

  • worked out, thanks!!!

  • Taking advantage of your kindness, you have a problem. If there are 5 records with the observation, it does not work, it only checks the first and adds in the other listview, the rest it does not add, because it will be ?

  • Because the function only looks for a subitem, make a loop for or while and run it, so go get all the subitens

  • I did it, see, that’s not correct ? http://pastebin.com/qhRx5atm

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.