简体   繁体   English

在Android应用程序(Java和XML)中使用C ++ Struct?

[英]Using a C++ Struct in Android app (Java and XML)?

I'm a decent C++ programmer, good enough to do what I want. 我是一个体面的C ++程序员,足以胜任我想要的工作。 But I'm working on my first Android App (obviously not C++ related), and I'm having an issue where I'd like to translate what I know from C++ over to the XML/Java used in Android Studio. 但是我正在开发我的第一个Android应用程序(显然与C ++无关),并且遇到了一个问题,我想将我从C ++转换为Android Studio中使用的XML / Java。

Basically I have (in C++) an array of structures. 基本上我(在C ++中)有一个结构数组。 And maybe I didn't do the perfect search, but I sure as heck tried to look around for the answer, but I didn't come up with anything. 也许我并没有进行完美的搜索,但是我确信当赫克试图四处寻找答案时,我什么都没想到。

How would I go about placing an array of structures inside the XML file and utilizing it in Java? 我如何在XML文件中放置一组结构并在Java中使用它?

As a bit of a buffer, let me say that I'm not really looking for code, just verification that this is possible, and a method on how to go about it. 作为一个缓冲区,让我说我不是在寻找代码,只是验证这是可行的,以及如何解决它的方法。 I don't mind researching to learn what I want, but I haven't come up with anything. 我不介意研究学习我想要的东西,但我没有想出任何东西。 Like I said, I probably haven't googled it properly because I'm unsure of exactly how to ask it. 就像我说的那样,我可能没有正确搜索它,因为我不确定如何问它。

EDIT: So it appears that XML doesn't have a structure (or anything similar? not sure). 编辑:看来XML没有结构(或类似的东西?不确定)。 But I can utilize a Java class with public variables. 但我可以使用带有公共变量的Java类。 Now my question is more or less: What would be the best way to go about inserting all the information into the array/class/variables? 现在我的问题或多或少:将所有信息插入数组/类/变量的最佳方法是什么?

In C++ terms, I could neatly place all the info into a text file and then read from it, using a FOR loop to place all the info in the structures. 在C ++术语中,我可以将所有信息整齐地放入文本文件中然后从中读取,使用FOR循环将所有信息放在结构中。 Or, if I don't want to use an outside source/file, I could hardcode the information into each variable. 或者,如果我不想使用外部源/文件,则可以将信息硬编码到每个变量中。 Tedious, but it'd work. 乏味,但可以。 I'm not sure, in Android terms, if I could use the same method and pack in a text file with the app, and read from the file using a FOR loop to insert the information into the array/class/variables 我不确定,就Android而言,我是否可以使用相同的方法并将其打包到应用程序的文本文件中,然后使用FOR循环从文件中读取信息以将信息插入到数组/类/变量中

class answerStruct
{
    public String a;

    public boolean status;

};



class questionStruct
{
    public String q;

    answerStruct[] answer = new answerStruct[4];

};

I'm not placing this here to brag at my super high tech program, but to give a visual, and frankly that's less I have to write out. 我不会把它放在这里吹嘘我的超高科技计划,但要给出一个视觉,坦率地说,我不得不写出来。 This is the method I plan on going with. 这是我计划的方法。 But, being Java, I'm open to possibly better options. 但是,作为Java,我对可能更好的选择持开放态度。 My question still stands as far as inputting information into the variables. 就将信息输入变量而言,我的问题仍然存在。 Hard code? 硬代码? or does Android/Java allow me to place a text file with my app, and read from it into the variables? 还是Android / Java允许我在应用程序中放置文本文件,然后将其读取到变量中?

XML is just a markup language for tree-structured data, and imposes no restrictions on how you name or structure your tree nodes. XML只是树结构数据的标记语言,对命名或构造树节点的方式没有任何限制。

What I think that you're looking for is an XML Object Serialiser : a way to serialise your in-memory structure into XML for a more permanent storage, and then at a later run, deserialise it back into memory. 我认为您正在寻找的是XML Object Serialiser序列化XML Object Serialiser :一种将内存中的结构序列化为XML的方式,以提供更永久的存储,然后在以后运行时,将其反序列化回内存。 There are many XML Serialisers for Java , each with an own proprietary XML format. 有许多XML Serialisers for Java ,每个都有自己的专有XML格式。

I've used Simple XML in the past, and found it easy and flexible. 我过去使用过Simple XML ,发现它简单灵活。

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

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