java - 如何从 Android 中 XML 中定义的字符串数组中获取对单个元素的引用

标签 java android arrays android-resources

我有一个用 XML 定义的非常庞大的字符串数组;例如

<string-array name="Descriptions">
    <item>A good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\nGiants are weak against many defenses grouped together.\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>
    <item>A good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\nGiants are weak against many defenses grouped together.\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>
    <item>A good raiding strategy is to fill your Army Camp with Giants and Archers; together they can easily wipe out a base. Use the Giants to distract all the Defensive Towers while the Archers take everything down.\nAt the beginning of a Raid, send in several Giants closest to the Mortar(s)/Wizard Tower(s). As soon as the Mortar hits the Giants, send in the necessary number of Wall Breakers to help the Giants break through the walls. Once the Mortar(s) and Wizard Tower(s) are destroyed, you can send in swarms of lower hit point Troops such as Goblins and Archers.\nGiants have a lot of health but are slow and have low attack power, so use them in groups.\nGiants are weak against many defenses grouped together.\nYou can use Giants as a cheap meat shield while deploying other, higher damaging troops to destroy buildings.\nAnother strategy is to send many Giants with one or more Healers. Be wary of Air Defenses and other anti-air towers, however, as Healers are very weak and have many vulnerabilities. \nIn general, Giants make better distracting troops than Barbarians due to their much higher health. Because of this they are less vulnerable to Mortar fire and other splash damage, and are much harder to kill.\nGiants ignore defensive troops (Clan Castle Troops, Barbarian King and Archer Queen) while defensive buildings can be targeted. Be sure to eliminate defensive troops to protect Giants.</item>

类似于示例,我的数组中有大量字符串,ARRAY 中有近 250 个这样的字符串元素。但是我只需要将一个项目加载到我的 android 项目中,是否可以这样做。

我知道,我能行

String[] k=getResources().getStringArray(R.array.Descriptions);
String Required=k[10];

但它会消耗大量内存,因为我正在加载大量垃圾。所以我只需要将该数组中的单个项目(假设我只需要加载第 10 个元素)加载到我的项目中,以使其更有效率。

我正在准备一个动态应用程序,因此我需要根据用户的需要从该数组加载不同的字符串。

有什么办法吗,谢谢

最佳答案

我认为您是在谈论 resource xml 文件,对吗?如果我说的不对,请完全忽略我的回答。

如果我是对的,那么目前建议的答案对您没有帮助。原因是apk文件中没有打包资源文件。它们被预构建为二进制格式,因此您无法查询它们。我的建议是重新考虑使用资源文件的方法,但使用本地数据库 (SQLite)。这样,您将加载到内存中的字符串将只是您查询数据库的字符串。 另一种方法(如果您仍想使用 xml 格式)是将其存储为 asset 而不是 resource 文件。 Assets 在 apk 中交付,因此使用 SaxParserXPathApis 的方法将很有用。

希望这对您有用并帮助您解决问题:)

关于java - 如何从 Android 中 XML 中定义的字符串数组中获取对单个元素的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27542806/

相关文章:

java - 将 Activity 类更改为 Fragment

java - 如何使用扫描仪库并使用嵌入式扫描仪在 Android 应用程序的后台进行扫描?

php - 动态地将值插入到带有索引的数组中

php - 搜索数组并在找到匹配时返回所有键和值

java - 为什么没有发送 cookie 时 request.getCookies() 返回 null?

java - Hystrix - 如何计算线程池大小

java - Vaadin - 如果我使用 AbsoluteLayout,则不会显示表数据

android - 调用 Looper.quit() 时崩溃

java - 我可以(轻松地)使用第三方库来处理 Java RMI 的序列化吗?

javascript - 将具有相同属性的两个对象的方法合并到每个属性的数组中