-3

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.

Arioch 'The
  • 15,799
  • 35
  • 62
  • 1
    If you want to return an array, return an array. Are you asking which type to use? Are you asking how to populate an array? Are you asking how to read the array values from somewhere else? – David Heffernan Jun 09 '15 at 11:18

2 Answers2

2

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;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
LU RD
  • 34,438
  • 5
  • 88
  • 296
  • that was fast, many thanks, however the IDs in a listview, is there a way to push theme as an array?? @LU RD – Mark Cooper Jun 09 '15 at 11:07
  • @MarkCooper 1) that s another question! 2) there are very different ListViews so impossible to say what are you talking about http://docwiki.embarcadero.com/Libraries/XE8/en/Vcl.ComCtrls.TListView and http://docwiki.embarcadero.com/Libraries/XE8/en/FMX.ListView.TListView – Arioch 'The Jun 09 '15 at 11:14
  • 1
    @Arioch'The VCL isn't going to be much use for an Android app – David Heffernan Jun 09 '15 at 11:21
  • @MarkCooper, why not iterate the listview? – LU RD Jun 09 '15 at 11:23
  • guess he has t use TStringList instead of array and then LiveBindings to map it onto ListView - if he does not want to right few lines of the loop code :-) – Arioch 'The Jun 09 '15 at 11:31
1

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/

Arioch 'The
  • 15,799
  • 35
  • 62
  • i have Delphi XE8, however the IDs in a listview, is there a way to push theme as an array?? @Arioch 'The – Mark Cooper Jun 09 '15 at 11:13
  • @Arioch You can tell it's a very modern Delphi from the Android references. – David Heffernan Jun 09 '15 at 11:18
  • ListView is 2D table, array is 1D vector, so you would have to make a loop or to choose a more suitable component. like Listbox, StringGrid, etc example of populating one of two listviews from an array http://docwiki.embarcadero.com/CodeExamples/XE8/en/RTL.AttributesAndRTTI_Sample – Arioch 'The Jun 09 '15 at 11:19
  • @DavidHeffernan perhaps, but not necessarily. If I download data from URL containing android - it is only a hint that my program MAYBE works on android, but that is not granted – Arioch 'The Jun 09 '15 at 11:20
  • @MarkCooper OTOH you can always add your method to TListView which would populate it from the array in exactly the way you want it – Arioch 'The Jun 09 '15 at 11:24
  • cuz i retrieved the IDs from Kinvey Provider and list theme in a listview!!, is there a better idea so i can pass the IDs as an Array @ Arioch 'The @ David Heffernan @LU RD – Mark Cooper Jun 09 '15 at 11:39
  • 1. what do you want ? delete items in FMX ListView and populate it from StringList/array ? or delete all items inm list/array and populate the said array form ListView? – Arioch 'The Jun 09 '15 at 11:41
  • just populate the itemsfrom the listview in the array @ Arioch 'The – Mark Cooper Jun 09 '15 at 11:43
  • 2. Anyway, that is ANOTHER question. This one is about the function returning array - and you have an answer here. If you have MORE questions, you are welcome to ask those OTHER questions as new questions. And please, when you write your question, after the questions there go tags - specify that you use `XE8` and `Firemonkey` library making your app for `Android` - otherwise you would get answers fit for traditional Delphi Win32 VCL applications wasting your time and our times too. – Arioch 'The Jun 09 '15 at 11:44
  • ok Mr Arioch 'The thanks fro your cooperation, if there any other question ill post theme, many thanks @ Arioch 'The – Mark Cooper Jun 09 '15 at 11:47