Android中通过代码获取arrays.xml文件中的数据
android工程res/valuse文件夹下的arrays.xml文件中用于放各种数组数据,比如字符串数组、整型数组等,数组中的数据可能是具体的值,也有可能是对资源数据的引用,下面针对这两种情况通过代码来获取相应的数组数据。
一、数组中的数据为具体的值:
比如arrays.xml文件中的数组如下:
<string-array name="select_dialog_items">
<item>Command one</item>
<item>Command two</item>
<item>Command three</item>
<item>Command four</item>
</string-array>
代码中获取该数组资源的具体代码如下:
String[] items = getResources().getStringArray(R.array.select_dialog_items);
items数组中的数据就是arrays.xml文件中对应资源id R.array.select_dialog_items中的数据;
数据为其它类型的数组也可以通过Resources类中相应的方法获取,比如:
获取整型数组的数据方法为:
public int[] getIntArray(int id) throws NotFoundException
获取文本数组的数据方法为:
public CharSequence[] getTextArray(int id) throws NotFoundException
二、数组中的数据为对资源数据的引用:
比如arrays.xml文件中的数组如下:
<string-array name="feed_icons">
<item>@drawable/latest</item>
<item>@drawable/video</item>
<item>@drawable/world</item>
<item>@drawable/sports</item>
<item>@drawable/arts</item>
<item>@drawable/dining</item>
</string-array>
代码中获取该数组资源的具体代码如下:
TypedArray typedArray = getResources().obtainTypedArray(R.array.feed_icons);
String[] titleArr = getResources().getStringArray(R.array.feed_names);
if( null != titleArr ){
int titleLength = titleArr.length;
for( int index = 0; index < titleLength; index++ ){
int feedResId = typedArray.getResourceId( index, 0 );
//...
}
}
获取arrays.xml中数据项为引用资源数据的数组时,首先通过Resources类中的obtainTypedArray方法获取到TypedArray实例,然后通过TypedArray方法中的getResourcesId方法获取数组中每一项的资源id,这样就能顺利地引用到数组中资源了。
通过代码获取arrays.xml中的数组资源时,数组中的元素项不宜过多,特别是一次性获取的时候,有可能你在使用时它还没有获取到你需要使用的数组项。
END
注意事项
通过android的arrays.xml获取数组信息时,在代码中初始化数组时建议通过线程初始化,避免在初始化的时候阻塞UI线程。
Android中通过代码获取arrays.xml文件中的数据的更多相关文章
- arrays.xml文件中添加drawable数组的问题
一.问题描述 今天遇到一个需求,将java中的数组搬进arrays.xml文件中 R.drawable.menu_share_pic_item, R.drawable.menu_share_wecha ...
- 在Android中把内容写到XML文件中
在Android中把内容写到XML文件中 saveXmlButton.setOnClickListener(new OnClickListener() { @Override public void ...
- vue项目中使用bpmn-流程图xml文件中节点属性转json结构
内容概述 本系列“vue项目中使用bpmn-xxxx”分为七篇,均为自己使用过程中用到的实例,手工原创,目前陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意事项,具 ...
- 获取AndroidManifest.xml文件中的meta-data
以获取高德地图的key值为例 <meta-data android:name="api_key" android:value="l8o0DhNxmvPDpCxTab ...
- Android使用xml文件中的array资源
Android中有种使用数组的非常简单的用法,在xml文件中获取. 创建数组资源 在value目录下创建arrays.xml文件 然后在arrays.xml文件中使用<string-array& ...
- Struts2 | struts.xml文件中使用method属性和通配符简化action标签和Action处理类的编写
转自:https://www.jianshu.com/p/310e89ee762d 在Struts2框架中,我们知道基本的Action标签只能实现一个url请求对应一个Action处理类.那么我们如果 ...
- 修改Android中strings.xml文件, 动态改变数据
有些朋友可能会动态的修改Android中strings.xml文件中的值,在这里给大家推荐一种简单的方法.strings.xml中节点是支持占位符的,如下所示: <string name=&qu ...
- VBScript Sample:遍历文件夹并获取XML文件中指定内容
案例: 我有一个文件夹,里面有很多子文件夹,每个子文件夹中都存在一个相同名字的XML文件,XML文件里面的标签结构相同,只是内容不同,XML文件中包含ID,Name等标签. 文件夹及文件结构如下图: ...
- Android之获取string.xml文件里面的方法
获取string.xml文件里面的方法 在此做个笔记: 1.在AndroidManifest.xml与layout等xml文件里: android:text="@string/resourc ...
随机推荐
- UltraChart导出图片
? //一定要先绑定UltraChart,如果先绑定,然后有点击图片导出,没有用的 string fulPath="xxxx"; this.UltraChartTScore.Sav ...
- Linux下Bash运行脚本
命令行应该这样写: sh -c "脚本字符串" example: sh -c "if ! type dpkg > /dev/null ; then echo 'so ...
- 【转】Android下编译jni库的二种方法(含示例)
原文网址:http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html 总结如下:两种方法是:1)使用Android源码中的Make系统2)使用NDK(从N ...
- cf486A Calculating Function
A. Calculating Function time limit per test 1 second memory limit per test 256 megabytes input stand ...
- UVa10340.All in All
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- SimHash算法
短文本合并重复(去重)的简单有效做法 - 旁观者 - 博客园 短文本合并重复(去重)的简单有效做法 SimHash算法 - ACdreamer - 博客频道 - CSDN.NET SimHash算法
- 《Java程序员面试笔试宝典》之Java变量命名有哪些规则
在Java语言中,变量名.函数名.数组名统称为标识符,Java语言规定标识符只能由字母(a~z,A~Z).数字(0~9).下划线(_)和$组成,并且标识符的第一个字符必须是字母.下划线或$.此外,标识 ...
- poj 2229 Sumsets(dp 或 数学)
Description Farmer John commanded his cows to search . Here are the possible sets of numbers that su ...
- (转)Eclipse 远程调试 WebSphere Application Server (WAS)
目前我们项目中使用的应用服务器多是WebSphere,一直苦于无法进行调试,今天在网上看到一篇,原文是 http://www.cnblogs.com/newstar/archive/2010/04/1 ...
- pip安装lxml报错
报错信息 解决方法:`` ...