Listview does not repeat records

2

I have a txt file with multiple records inside, I need something that at the time the function is to add to the ListView these records, do not let me add repeated items. To do this, it could use a particular column and compare. Within the txt the delimiter I use is an @.

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;
    
asked by anonymous 31.08.2014 / 20:15

1 answer

2

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

Change the Colunas[2] by the desired column in the comparison in the parts dealing with 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;
    
06.09.2014 / 05:17