我以以下方式填充 DrawerListView:
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
getResources().getStringArray(R.array.dataElements)));
列表抽屉已正确填充并按预期显示数据。但是,我不想引用 R.array.dataElements,而是想引用我下载到 ExternalStorageDirectory 的文件 (list.xml)。如何获取此下载文件的资源 ID,以便填充 ListView?
- 更新 -
我能够使用以下代码达到预期的结果。感谢您为我指明正确的方向。
try {
myArray = new ArrayList<String>();
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/list.xml");
InputStream instream = new FileInputStream(file.getPath());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(instream));
doc.getDocumentElement().normalize();
// Parse XML file and store in string
NodeList myNodeList = doc.getElementsByTagName("item");
for (int i = 0; i < myNodeList.getLength(); i++) {
Node myNode = myNodeList.item(i);
if(myNode.getNodeType() == Node.ELEMENT_NODE){
myArray.add(myNode.getTextContent());
}
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}