1

次のコードを使用して nameList を取得します

nameList の内容は Inbox、Sent、Outbox、Draft になると思います、それが私の願いです。
しかし実際には、nameList の内容は @string/Inbox、@string/Sent、@string/Outbox、および @string/Draft です。なんで?ありがとう!

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();
    }
}

値\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>
4

1 に答える 1

4

他のリソース タイプとは異なり、android は res/xml リソース内のリソースを自動的に解決しません。だからあなたはそれをするように言わなければなりません。

最初に、解析された属性にリソースが含まれているかどうかを確認する必要があります。

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

stringResId がある場合、ゼロ以外になります。ゼロの場合、属性値にリソース識別子はありません。

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...
}

したがって、ここで重要なのは、属性値をリソースとして解決する getAttributeResourceValue を確認することです (リソースが 1 つの場合)。

お役に立てれば...

于 2013-06-23T21:05:17.010 に答える