Listview does not repeat records

Asked

Viewed 674 times

2

I have a file txt with multiple records inside, I need something that at the time the function is added to the ListView these records, do not let repeat items be added. To do this, it could use a certain column and compare. Inside the txt the delimiter I use is a @.

Code:

 var
  Linhas:  TStringList;
  Colunas: TStringList;
  i,l:       integer;
  Item: TListItem;
  begin
  Linhas := TStringList.Create;
  Colunas := TStringlist.Create;
  Linhas.LoadFromFile('c:\clientes.txt');

        for i := 0 to Linhas.Count-1 do
        begin
           Colunas.Text := StringReplace(Linhas[i],'@',Char(13)+Char(10),[rfReplaceAll]);
           Item := Form1.LV.Items.Add;
           l := l + 1;
           Item.Caption := inttostr(l);
           Item.SubItems.Add(Colunas[1]);
           Item.SubItems.Add(Colunas[0]);
           Item.SubItems.Add(Colunas[7]);
           Item.SubItems.Add(Colunas[2]);
        end;
        end;
  • Try Subitems.Indexof(Columns[1]); if it returns < 0 it means it did not, if it returns >= 0 it means it has found

  • @Passella, and if he finds repeats, how can I make him erase all leaving only 1 ?

  • you have to do this check when insert the item

  • See friend @Passella: if Item.SubItems.Indexof(Columns[2]) <= 0 then Begin Item.Caption := inttostr(l); Item.SubItems.Add(Columns[1]); Item.SubItems.Add(Columns[0]);

  • It didn’t work, he’s adding repeated!

  • 1

    show your entire code, not much to do without seeing it.

  • @Passella, the code is the same and up, with the difference that I entered the rule that you passed before the Item.Caption, you understand ?

  • @Passella, any suggestions ? our getting a little too caught up in this..

Show 3 more comments

1 answer

2

Follow a solution using an auxiliary list to avoid scanning the TListView whole. You can optimize the code later, according to the practical use you will make.

Exchange the Colunas[2] by the column desired in the comparison in the parts dealing with the ListaAuxiliar.

var
   Linhas:        TStringList;
   Colunas:       TStringList;
   ListaAuxiliar: TStringList;
   i, l:          integer;
   Item:          TListItem;
begin
   Linhas        := TStringList.Create;
   Colunas       := TStringlist.Create;
   ListaAuxiliar := TStringList.Create;
   Linhas.LoadFromFile( 'c:\clientes.txt' );
   for i := 0 to Linhas.Count - 1 do
   begin
      Colunas.Text := StringReplace( Linhas[i], '@', Char(13) + Char(10), [rfReplaceAll] );

      if ListaAuxiliar.IndexOf( Colunas[2] ) = -1 then
      begin
         ListaAuxiliar.Add( Colunas[2] );
         Item := Form1.LV.Items.Add;
         l := l + 1;
         Item.Caption := inttostr( l );
         Item.SubItems.Add( Colunas[1] );
         Item.SubItems.Add( Colunas[0] );
         Item.SubItems.Add( Colunas[7] );
         Item.SubItems.Add( Colunas[2] );
      end;
   end;
end;
  • Unfortunately it didn’t work, he keeps repeating the items..... I tried to pick up by the value that would be Column[4] and even then it didn’t work.

  • Add your question a "customers.txt" example, please.

  • @user7605 remembered to change the Columns[4] in the two places?

Browser other questions tagged

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