该@strings不所在的文件夹资源xml文件的工作\\ XML \\ [英] The @strings don't work in the xml file located the folder res\xml\

查看:128
本文介绍了该@strings不所在的文件夹资源xml文件的工作\\ XML \\的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code得到一个名称列表

我觉得名称列表的内容将收件箱,已发送,发件箱和草稿,这是我的心愿。结果
但事实上,名称列表的内容是@字符串/收件箱,@字符串/发送,@字符串/发件箱和@字符串/草案。为什么?谢谢!

 私人无效InitVar(){
    ArrayList的名称列表=新的ArrayList<串GT;();
    ArrayList的valueList =新的ArrayList<串GT;();
    ParXML(这一点,名称列表,valueList);
}公共静态无效ParXML(上下文的背景下,列表与LT;弦乐>名称列表,列表<串GT; valueList){
    。XmlResourceParser XRP = context.getResources()的getXML(R.xml.msgfolder);
    尝试{
        而(xrp.getEventType()!= XmlResourceParser.END_DOCUMENT){
            如果(xrp.getEventType()== XmlResourceParser.START_TAG){
                字符串标记名= xrp.getName();
                如果(tagName.equals(项目)){
                    nameList.add(xrp.getAttributeValue(NULL,名));
                    valueList.add(xrp.getAttributeValue(NULL,值));
                }
            }
            xrp.next();
        }
    }赶上(XmlPullParserException E){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }
}

值\\的strings.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
    <字符串名称=收件箱>收件匣LT; /串>
    <字符串名称=已发送>发< /串>
    <字符串名称=发件箱>发件箱< /串>
    <字符串名称=草案>&草案LT; /串>
< /资源>

XML \\ msgfolder.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< MsgHandle>
   <项目名称=@字符串/收件箱VALUE =内容://短信/收件箱>< /项目>
   <项目名称=@字符串/发送值=内容://短信/发送>< /项目>
   <项目名称=@字符串/发件箱VALUE =内容://短信/发件箱>< /项目>
   <项目名称=@字符串/草案VALUE =内容://短信/草案>< /项目>
< / MsgHandle>


解决方案

与其他资源类型,Android不自动在res / xml的资源,解决资源。所以,你必须告诉它做的事。

首先,你必须检查是否解析属性有资源在它:

  INT stringResId = xrp.getAttributeResourceValue(NULL,名,0);

,如果它有stringResId将不为零。如果它是零,那么有在属性值没有资源标识符

 如果(stringResId == 0){
    nameList.add(xrp.getAttributeValue(NULL,名)); //< - 获取属性值的字符串...
}其他{
    nameList.add(context.getResources()的getString(stringResId)); //< - 获取由stringResId标识字符串资源...
}

因此​​,这里的关键是要检查与解决作为资源属性值的getAttributeResourceValue(如果它是一个)。

希望这有助于...

I use the following code to get a nameList

I think the content of nameList will be Inbox, Sent, Outbox and Draft, it's my wish.
but in fact, the content of nameList is @string/Inbox, @string/Sent, @string/Outbox and @string/Draft. Why? Thanks!

private void InitVar(){
    ArrayList nameList=new ArrayList<String>();
    ArrayList valueList=new ArrayList<String>();
    ParXML(this, nameList, valueList);                              
}

public static void ParXML(Context context, List<String> nameList,List<String> valueList) {
    XmlResourceParser xrp = context.getResources().getXml(R.xml.msgfolder);
    try {
        while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
            if (xrp.getEventType() == XmlResourceParser.START_TAG) {
                String tagName = xrp.getName();
                if (tagName.equals("item")) {
                    nameList.add(xrp.getAttributeValue(null, "name"));
                    valueList.add(xrp.getAttributeValue(null, "value"));
                }
            }
            xrp.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Values\strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="Inbox">Inbox</string>
    <string name="Sent">Sent</string>
    <string name="Outbox">Outbox</string>
    <string name="Draft">Draft</string>   
</resources>

xml\msgfolder.xml

<?xml version="1.0" encoding="utf-8"?>
<MsgHandle>
   <item name="@string/Inbox" value="content://sms/inbox"></item>
   <item name="@string/Sent" value="content://sms/sent"></item>
   <item name="@string/Outbox" value="content://sms/outbox"></item>
   <item name="@string/Draft" value="content://sms/draft"></item>
</MsgHandle>

解决方案

Unlike other resource types, android doesn't resolve resources automatically in res/xml resources. So you have to tell it to do.

First you have to check if the parsed attribute has a resource in it:

int stringResId = xrp.getAttributeResourceValue(null, "name", 0);

if it has stringResId will be non zero. If it is zero then there is no resource identifier in the attribute value.

if (stringResId == 0) {
    nameList.add(xrp.getAttributeValue(null, "name")); //<-- Get attribute value as string...
} else {
    nameList.add(context.getResources().getString(stringResId)); //<-- Get string resource identified by the stringResId...
}

So the key here is to check with the getAttributeResourceValue which resolves the attribute value as resource is (if it is one).

Hope this helps...

这篇关于该@strings不所在的文件夹资源xml文件的工作\\ XML \\的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆