android - Load resources with variables? -
i loading xml file resource this,
getresources().getxml(r.xml.fiel1); now, scenario depending on factors there may many xml files choose from. how do that? in case filename similar in fact starts file ends different numbers file1, file2,file3 etc., can form string variable file name , add suffix per requirement form filename file1 (file+1). problem keep getting various errors (nullpointerex, resourceid not found etc) in whatever way try pass filename variable method. correct way of accomplishing this?
you use getidentifier() docs mention:
use of function discouraged. more efficient retrieve resources identifier name.
so it's better use array references xml files. can declare integer array resource. eg, in res/values/arrays.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <integer-array name="xml_files"> <item>@xml/file1</item> <item>@xml/file2</item> etc... </integer-array> </resources> and in java:
private xmlresourceparser getxmlbyindex(int index) { resources res = getresources(); return res.getxml(res.getintarray(r.array.xml_files)[index - 1]); } of course, you'll need update array whenever add new xml file.
Comments
Post a Comment