2015-06-09 29 views
-2

你能幫助我與此功能如何修改了一項功能,它返回一個字符串數組

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方法。

+1

如果你想返回一個數組,返回數組。你問是使用哪種類型?你問如何填充數組?你問如何從其他地方讀取數組值? –

回答

2

一個實例如何從的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; 
+0

很快,非常感謝,但是在列表視圖中的ID,有沒有一種方法來推動主題爲一個數組? @LU RD –

+0

@MarkCooper 1)這是另一個問題! 2)有非常不同的ListViews,所以無法說出你在說什麼http://docwiki.embarcadero.com/Libraries/XE8/en/Vcl.ComCtrls.TListView和http://docwiki.embarcadero.com/Libraries/ XE8/EN/FMX.ListView.TListView –

+1

@ Arioch'The VCL不會成爲Android應用 –

1

在Delphi的情況下2010+可以使用團結會的回答(和您做的更好 - 對於寬鬆類型的兼容性)

在早期的Delphi的情況下,你必須使用另一種類型:

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

而且是獨立於德爾福RTL你可以聲明類型自己

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

PS。我知道正式德爾福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的懶惰的填充從1D字符串矢量一cn使用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/

+0

循環碼的右側幾行,我有德爾福XE8,但是在ListView的ID,是有一種將主題推送爲數組的方法? @Arioch' –

+0

@Arioch你可以從Android引用中知道它是一個非常現代的Delphi。 –

+0

ListView是2D表格,數組是1D矢量,所以你必須做一個循環或者選擇一個更合適的組件。像列表框,StringGrid等填充2個列表視圖中的一個從一個數組 http://docwiki.embarcadero.com/CodeExamples/XE8/en/RTL.AttributesAndRTTI_Sample –

相關問題