Search code examples
delphifiremonkey

Populating a ListView at run-time


I'm trying to populating a ListView at run-time with DynamicAppearance. The text parts work well, but the image is always the same as the last one. This code adds to all items the same image. I want to add a image to just one item.


procedure TfrmMain.crialista;
var
Aux1 : TStringDynArray; // Usado para trocar o nome.exe por nome_Icon
icone : string;          // Recebe o caminho completo para pegar o icone
Programa : Tprograma;  //  Contem todas as informações do programa , é passado para cada item da listbox.
Path : string;   //acho que não serve de nada
    // tb não usa pra nada
  Aux : integer;
 ARPrograma : array of TPrograma ;
  searchaux: string;
  Contador : integer;
begin
     listview1.Items.clear;

          TThread.CreateAnonymousThread(procedure
     begin
      QryEIS.Close;
      QryEIS.open; 
        Contador:= 0 ;

       SetLength(ARPrograma, QRyEIS.RecordCount );
      QryEIS.First;        
      while not QryEIS.Eof do
        begin
          QryDiretorio.Close;

          QryDiretorio.ParamByName('NomeDoSistema').Value:= QryEIS.FieldByName('DescricaoEIS').Value;
             QryDiretorio.Open;
          // Pegando o Icone do programa...

          if QryDiretorio.RecordCount = 0  then //escrever o erro aqui
            else
            begin

          aux1 :=     splitString( QryDiretorio.FieldByName('Sistema').Value, '.')  ;
          icone := 'F:\Sistemas\Icones\Launcher\' +aux1[0]+'_Icon.ico';
         // ImageList1.AddMasked(Bmp,clFuchsia);
          imagecontrol1.LoadFromFile(icone);
          ARPrograma[Contador].Icone := imagecontrol1.bitmap;


          ARPrograma[Contador].Nome:= QryDiretorio.FieldByName('NomeDoSistema').Value;
          ARPrograma[Contador].NomeExe :=  QryDiretorio.FieldByName('Sistema').Value;
             ARPrograma[Contador].SingleUser := QryDiretorio.FieldByName('SingleUser').Value ;
          // Caminho da unidade C (PathC) e da unidade F (Path)
          if QryDiretorio.FieldByName('SingleUser').Value = 'T' then
          begin
        ARPrograma[Contador].path := trim(QryDiretorio.FieldByName('Origem').Value)+'\'+trim(QryDiretorio.FieldByName('Sistema').Value);
            ARPrograma[Contador].pathC := trim(QryDiretorio.FieldByName('destino').Value)+'\'+qryNome.FieldByname('NomeUsuario').Value+'\'+trim(QryDiretorio.FieldByName('Sistema').Value);
            ARPrograma[Contador].DiretorioUser := trim(QryDiretorio.FieldByName('destino').Value)+'\'+qryNome.FieldByname('NomeUsuario').Value;
             ARPrograma[Contador].Diretorio   :=trim(QryDiretorio.FieldByName('destino').Value);
             ARPrograma[Contador].SingleUser := QryDiretorio.FieldByName('SingleUser').Value ;

          end
          else
          begin
          ARPrograma[Contador].path := trim(QryDiretorio.FieldByName('Origem').Value)+'\'+trim(QryDiretorio.FieldByName('Sistema').Value);
          ARPrograma[Contador].pathC := trim(QryDiretorio.FieldByName('destino').Value)+'\'+trim(QryDiretorio.FieldByName('Sistema').Value);
          ARPrograma[Contador].Diretorio := trim(QryDiretorio.FieldByName('destino').Value);
          end ;



            aux := fileage(ARPrograma[Contador].path);
           ARPrograma[Contador].DataMod := FileDateToDateTime(aux);
         ARPrograma[Contador].Versao  :=  Sto_GetFmtFileVersion(Programa.path , '');
          if Programa.Versao <> '' then 
              begin
              aux1 := splitString (Programa.Versao , '.');
             ARPrograma[Contador].iVer[1] := strtoint(aux1[0]);
             ARPrograma[Contador].iVer[2] := strtoint(aux1[1]);
              ARPrograma[Contador].iVer[3] := strtoint(aux1[2]);
              ARPrograma[Contador].iVer[4] := strtoint(aux1[3]);
              end;


              if fileExists(programa.pathC) then
            begin


              ARPrograma[Contador].VersaoC := Sto_GetFmtFileVersion(Programa.pathC , '');
               if Programa.VersaoC <> '' then   //  Mesma coisa do comentario do F:
              begin
              aux1 := splitString (Programa.VersaoC , '.');
             ARPrograma[Contador].iVerC[1] := strtoint(aux1[0]);
              ARPrograma[Contador].iVerC[2] := strtoint(aux1[1]);
              ARPrograma[Contador].iVerC[3] := strtoint(aux1[2]);
              ARPrograma[Contador].iVerC[4] := strtoint(aux1[3]);
              end;
            end ;


        // Here i call the CriaFrama. 
          criaframa(ARPrograma[Contador], Contador);

           contador := Contador +1;
          end;


          QryEIS.Next;



          end;


         TThread.Synchronize(nil, procedure
         begin
// some visual thing....

         end);

     end).Start;
end;
procedure TfrmMain.CriaFrama(s: Tprograma; a : integer); 
var
  anitem : TListViewItem;
  txt : TListItemText;
  img : TListItemImage;
begin
  with FrmMain do
  begin
    anitem := Listview1.Items.Add;
    with anitem do
    begin
      txt := TListItemText(Objects.FindDrawable('Text1'));
      txt.Text := S.Nome;
      //the image  part
      img := TListItemImage(Objects.FindDrawable('Image2'));
      img.Bitmap := s.Icone;
    end;
  end;

Just for i can post the item...................

It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details.


Solution

  • First of all, thank so much for helping me, Remy Lebeau. As he said in the comments, the problem was in pointing all items to the same bitmap and not synchronizing the segment when changing something in the user interface. (Not sync generates some access violation errors) The code:

        procedure TfrmMain.crialista;
    var
    Aux1 : TStringDynArray; 
    icone : string;          
    Path : string;   
    Aux : integer;
    ARPrograma : array of TPrograma ;
    searchaux: string;
    Contador : integer;
    bitmap : FMX.Graphics.TBitmap;
    begin
    
         listview1.Visible := false;
     load_menu.Visible := true;
     Load_menu.Enabled :=  true;
         frmMain.LPrincipal.RemoveObject(0);
         listview1.Items.clear;
         searchaux:= searchBox1.Text;
         searchBox1.Text.Empty;
        TThread.CreateAnonymousThread(procedure
        var bitmap : FMX.Graphics.TBitmap;
         begin
          QryEIS.Close;
          QryEIS.open; 
           Contador:= 0 ;
           SetLength(ARPrograma, QRyEIS.RecordCount );
          QryEIS.First;        
          while not QryEIS.Eof do
            begin
              QryDiretorio.Close;
    
              QryDiretorio.ParamByName('NomeDoSistema').Value  := 
                QryEIS.FieldByName('DescricaoEIS').Value;
                 QryDiretorio.Open;
    
    
              if QryDiretorio.RecordCount = 0  then 
                else
                begin
    
                       TThread.Synchronize(nil, procedure
            begin
               aux1 :=splitString( QryDiretorio.FieldByName('Sistema').Value, '.')  ;
               icone := 'F:\Sistemas\Icones\Launcher\' +aux1[0]+'_Icon.ico';
               BitMap := FMX.Graphics.TBitmap.CreateFromFile(icone);
               ARPrograma[Contador].Icone := bitmap ;
    
            end);
    
    
    
              ARPrograma[Contador].Nome:= QryDiretorio.FieldByName('NomeDoSistema').Value;
              ARPrograma[Contador].NomeExe :=  QryDiretorio.FieldByName('Sistema').Value;
                 ARPrograma[Contador].SingleUser := QryDiretorio.FieldByName('SingleUser').Value ;
    
              if QryDiretorio.FieldByName('SingleUser').Value = 'T' then
              begin
            ARPrograma[Contador].path := trim(QryDiretorio.FieldByName('Origem').Value)+'\'+trim(QryDiretorio.FieldByName('Sistema').Value);
                ARPrograma[Contador].pathC := trim(QryDiretorio.FieldByName('destino').Value)+'\'+qryNome.FieldByname('NomeUsuario').Value+'\'+trim(QryDiretorio.FieldByName('Sistema').Value);
                ARPrograma[Contador].DiretorioUser := trim(QryDiretorio.FieldByName('destino').Value)+'\'+qryNome.FieldByname('NomeUsuario').Value;
                 ARPrograma[Contador].Diretorio   :=trim(QryDiretorio.FieldByName('destino').Value);
                 ARPrograma[Contador].SingleUser := QryDiretorio.FieldByName('SingleUser').Value ;
    
              end
              else
              begin
              ARPrograma[Contador].path := trim(QryDiretorio.FieldByName('Origem').Value)+'\'+trim(QryDiretorio.FieldByName('Sistema').Value);
              ARPrograma[Contador].pathC := trim(QryDiretorio.FieldByName('destino').Value)+'\'+trim(QryDiretorio.FieldByName('Sistema').Value);
              ARPrograma[Contador].Diretorio := trim(QryDiretorio.FieldByName('destino').Value);
              end ;
              //Pega a versão do .exe da unidade F:
    
    
                aux := fileage(ARPrograma[Contador].path); // Do not use that , use the 2 params FileAge, for more information check the comments
               ARPrograma[Contador].DataMod := FileDateToDateTime(aux);
             ARPrograma[Contador].Versao  :=  Sto_GetFmtFileVersion(Programa.path , '');
              if Programa.Versao <> '' then //caso não seja nulo , coloca no array de integer , para ficar mais facil de comparar na hora de atualizar. (fica em Udetalhes)
                  begin
                  aux1 := splitString (Programa.Versao , '.');
                 ARPrograma[Contador].iVer[1] := strtoint(aux1[0]);
                 ARPrograma[Contador].iVer[2] := strtoint(aux1[1]);
                  ARPrograma[Contador].iVer[3] := strtoint(aux1[2]);
                  ARPrograma[Contador].iVer[4] := strtoint(aux1[3]);
                  end;
    
                  if fileExists(programa.pathC) then
                begin
    
    
                  ARPrograma[Contador].VersaoC := Sto_GetFmtFileVersion(Programa.pathC , '');
                   if Programa.VersaoC <> '' then  
                  begin
                  aux1 := splitString (Programa.VersaoC , '.');
                  ARPrograma[Contador].iVerC[1] := strtoint(aux1[0]);
                  ARPrograma[Contador].iVerC[2] := strtoint(aux1[1]);
                  ARPrograma[Contador].iVerC[3] := strtoint(aux1[2]);
                  ARPrograma[Contador].iVerC[4] := strtoint(aux1[3]);
                  end;
                end ;
    
    
              TThread.Synchronize(nil, procedure
            begin
                 criaframa(ARPrograma[Contador], Contador);
    
            end);
    
    
               contador := Contador +1;
              end;
    
              QryEIS.Next;
    
    
    
              end;
    
    
            TThread.Synchronize(nil, procedure
            begin
    
              listview1.Visible := true;
              load_menu.Visible := false;
              Load_menu.Enabled :=  false;
               searchBox1.Text :='';
            end);
    
         end).Start;
    end;
    

    The other part still the same.