简体   繁体   English

如何从Android Kitkat Delphi XE8中的列表框提取原始JString值

[英]How to extract original JString value from Listbox in Android Kitkat Delphi XE8

I am making a test android multidevice application with Delphi xe8. 我正在用Delphi xe8做一个测试android多设备应用程序。 I am attaching objects to the items in a listbox as follows: 我将对象附加到列表框中的项目,如下所示:

 unit Unit1;

 interface

 uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,  
   System.Variants,

  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics,    
  Androidapi.JNI.JavaTypes, FMX.Dialogs,
 FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.ListBox,    
 Androidapi.Helpers;

 type
   TForm1 = class(TForm)
   ListBox1: TListBox;
   Button1: TButton;
   Button2: TButton;
   procedure Button1Click(Sender: TObject);
   procedure Button2Click(Sender: TObject);
   private
   { Private declarations }
   public
   { Public declarations }
   end;

     var
    Form1: TForm1;

    implementation
    {$R *.fmx}

    //this where I am Attaching objects to items
    procedure TForm1.Button1Click(Sender: TObject);
    var
    str:string;
    jstr1:JString;
    begin
    str:='apple';
    jstr1:=StringToJString(str);
    ListBox1.Items.AddObject('fruit', TObject(jstr1));
    end;


    //this where I am extracting the jstring objects
    procedure TForm1.Button2Click(Sender: TObject);
     var
    jstr2:JString;
    str2:string;
    begin
    jstr2:=JString(ListBox1.Items.Objects[i]);
    str2:=JStringToString(jstr2);
    showmessage('the fruit of the day is '+str2);
    end;

   end.

The above code run ok and the jstring objects are attached to the items, however, when I want extract the jstring object that has been attached to the item, I do this: 上面的代码运行正常,并且jstring对象已附加到项目中,但是,当我要提取已附加到该项目的jstring对象时,我会这样做:

 jstr2:=JString(ListBox1.Items.Objects[i]);
 //Above give me an AV: I get incompatible types TObject and JString

 str2:=JStringToString(jstr2);

The above code does not compile because of incompatible types TObject and JString. 上面的代码由于类型不兼容的TObject和JString而无法编译。 But yet if had attached a string as the object(instead of a jstring) and wanted to get back those string objects I could just do: 但是,如果将字符串作为对象(而不是jstring)附加并想要取回那些字符串对象,我可以这样做:

str2:=String(ListBox1.Items.Objects[i]);

This would work with regular strings. 这将与常规字符串一起使用。 How do I solve this problem, attach and extract jstring? 如何解决此问题,附加并提取jstring?

This is just a suggestion, and I can not check this (nothing installed here), but you could write a simple object type and store that (I put it in its own unit, which you then use in your form unit): 这只是一个建议,我无法检查(此处未安装任何东西),但是您可以编写一个简单的对象类型并将其存储(我将其放在自己的单元中,然后在表单单元中使用):

unit StringObjs;

interface

uses 
  Androidapi.JNI.JavaTypes;

type
  TStringObj = class
  private
    FPayload: string;
  public
    constructor Create(const Payload: JString);
    class operator Explicit(const Obj: TStringObj): string;
  end;

implementation

uses
  Androidapi.Helpers;

constructor TStringObj.Create(const Payload: JString);
begin
  FPayload := JStringToString(Payload); // store string, not JString!
end;

class operator TStringObj.Explicit(const Obj: TStringObj): string;
begin
  Result := Obj.FPlayload;
end;

end.

And you use it in your form unit's implementation section like: 然后在表单单元的实现部分中使用它,例如:

implementation

uses ..., StringObjs;

...

  ListBox1.Items.AddObject('fruit', TStringObj.Create(jstr1));

and reverse: 并反向:

  MyString := string(ListBox1.Items[0] as TStringObj);

Note that a form is just a class, but one that the IDE knows about and can edit using the form designer. 请注意,表单只是一类,但是IDE知道并且可以使用表单设计器进行编辑。 To declare other classes, do something similar as I do above. 要声明其他类,请执行与上面类似的操作。 Read the Delphi Language Guide to learn more. 阅读《 Delphi语言指南》以了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM