how to modify a function so it returns an array of strings

299 views Asked by At

can you help me to with this function

function TdmPush.GetDeviceRegistrationId: string;
begin
{$IFDEF ANDROID}
result := gcmn.RegistrationID;
{$ELSE}
result := 'Mobile Test';
{$ENDIF}
 end;


 function TdmPush.PushMessage(Pushmessage : string):string;
  const
  sendUrl = 'https://android.googleapis.com/gcm/send';
  var
   Params: TStringList;
   AuthHeader: STring;
   idHTTP: TIDHTTP;
   SSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
   begin
   idHTTP := TIDHTTP.Create(nil);
   try
   SslIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
   idHTTP.IOHandler := SSLIOHandler;
   idHTTP.HTTPOptions := [];
   Params := TStringList.Create;
    try
     Params.Add('registration_id='+ GetDeviceRegistrationId());
     Params.Values['data.message'] := Pushmessage;
     idHTTP.Request.Host := sendUrl;
     AuthHeader := 'Authorization: key=' + YOUR_API_ID;
      idHTTP.Request.CustomHeaders.Add(AuthHeader);
      IdHTTP.Request.ContentType := 'application/x-www-form-   urlencoded;charset=UTF-8';
     result := idHTTP.Post(sendUrl, Params);
      finally
      Params.Free;
   end;
  finally
    FreeAndNil(idHTTP);
 end;

 end;

I need the function GetDeviceRegeistrationID to return an array of registration id, so I can modify the Push method.

2

There are 2 answers

5
LU RD On

An example how to assign the text from the items of a TListView into an array of string.

function TdmPush.GetDeviceRegistrationId: TArray<string>;
var
  i: Integer;
begin
  SetLength(Result, myListView.Items.Count);
  ... 
  // Fill the array
  for i := 0 to myListView.Items.Count-1 do
    Result[i] := myListView.Items[i].Text;
end;
10
Arioch 'The On

In case of Delphi 2010+ you can use LURD's answer (and you better do - for relaxed types compatibility)

In case of earlier Delphi you have to use another type:

uses Types;
function TdmPush.GetDeviceRegistrationId: tStringDynArray;
///...the rest is the same as with LU RD...

Also to be independent from Delphi RTL you can declare the type yourself

type MyStringsArray = array of string;
function TdmPush.GetDeviceRegistrationId: MyStringsArray ;
///...the rest is the same as with LU RD...

PS. I know that formally Delphi 2009 also has TArray, but usng generics in 2009 is a ticket to hell.

PPS. if you can not know the exact number of strings in the array in advance, then for the sake of scaling heap memory management use special classes:

uses Generics.Collections;
function TdmPush.GetDeviceRegistrationId: TArray<string>;
var ls: TList<string>;
begin
   ls := TList<string>.Create;
   try
     ls.Add('aaaa');
     ls.Add('bbb');
     ls.Add('cccccc');
 ....
     ls.Add('$%#$#');

     Result := ls.ToArray();
   finally
     ls.Destroy;
   end;
end; 

To make a lazy filling of a ListView from the 1D vector of strings one cn utilize LiveBinding.

The general idea - http://docwiki.embarcadero.com/RADStudio/XE8/en/Mobile_Tutorial:_Using_LiveBindings_to_Populate_a_ListView_(iOS_and_Android)

Using TStringList as binding data source - http://www.webdelphi.ru/2011/11/firemonkey-ot-prostogo-k-slozhnomu-3-komponenty-fmx-spiski-prodolzhenie/