【问题标题】:how to modify a function so it returns an array of strings [closed]如何修改函数以使其返回字符串数组[关闭]
【发布时间】:2015-06-09 10:54:19
【问题描述】:

你能帮我完成这个功能吗

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;

我需要函数GetDeviceRegeistrationID返回一个注册id数组,所以我可以修改Push方法。

【问题讨论】:

  • 如果要返回一个数组,就返回一个数组。你问使用哪种类型?您是在问如何填充数组吗?您是在问如何从其他地方读取数组值?

标签: android delphi firemonkey delphi-xe8


【解决方案1】:

如何将文本从 TListView 的项目分配到字符串数组中的示例。

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;

【讨论】:

【解决方案2】:

在 Delphi 2010+ 的情况下,您可以使用 LURD 的答案(而且您最好这样做 - 用于宽松的类型兼容性)

如果是早期的 Delphi,您必须使用另一种类型:

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

还要独立于 Delphi RTL,你可以自己声明类型

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

PS。我知道正式的 Delphi 2009 也有 TArray,但 2009 年使用泛型是通往地狱的门票。

PPS。如果您无法提前知道数组中字符串的确切数量,那么为了扩展堆内存管理,请使用特殊类:

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; 

要从字符串的一维向量中懒惰地填充 ListView,可以使用 LiveBinding。

总体思路 - http://docwiki.embarcadero.com/RADStudio/XE8/en/Mobile_Tutorial:_Using_LiveBindings_to_Populate_a_ListView_(iOS_and_Android)

使用 TStringList 作为绑定数据源 - http://www.webdelphi.ru/2011/11/firemonkey-ot-prostogo-k-slozhnomu-3-komponenty-fmx-spiski-prodolzhenie/

【讨论】:

  • 我有 Delphi XE8,但是列表视图中的 ID,有没有办法将主题作为数组推送? @Arioch'The
  • @Arioch 您可以从 Android 参考资料中看出这是一个非常现代的 Delphi。
  • ListView 是二维表,数组是一维向量,所以你必须做一个循环或者选择一个更合适的组件。像 Listbox、StringGrid 等从数组 docwiki.embarcadero.com/CodeExamples/XE8/en/… 填充两个列表视图之一的示例
  • @DavidHeffernan 也许,但不一定。如果我从包含 android 的 URL 下载数据 - 这只是暗示我的程序可能在 android 上运行,但这是不被允许的
  • @MarkCooper OTOH 你总是可以将你的方法添加到 TListView 中,它会以你想要的方式从数组中填充它
猜你喜欢
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
相关资源
最近更新 更多