Java 从ListView中的XML资源文件添加项
我的strings.xml文件中有一个标题数组。我想将这些标题从XML文件添加到“活动”中的列表视图中。我该怎么做?请帮我举个例子。您可以将XML解析为ArrayList,然后将ArrayList应用于ListView适配器。它可能会像这样工作:Java 从ListView中的XML资源文件添加项,java,android,xml,listview,android-resources,Java,Android,Xml,Listview,Android Resources,我的strings.xml文件中有一个标题数组。我想将这些标题从XML文件添加到“活动”中的列表视图中。我该怎么做?请帮我举个例子。您可以将XML解析为ArrayList,然后将ArrayList应用于ListView适配器。它可能会像这样工作: // create an arraylist object to add all the string values from the xml ArrayList titles = new ArrayList(); // populate your a
// create an arraylist object to add all the string values from the xml
ArrayList titles = new ArrayList();
// populate your array
XMLEncoder xmlEncoder = new XMLEncoder(BufferedOutputStream(
new FileOutputStream("titles.xml")));
xmlEncoder.writeObject(titles);
xmlEncoder.close();
// create another arraylist that will get applied to list view
ArrayList filteredTitles = new ArrayList();
// sort through all the xml strings in the first xml arraylist
// make sure were only getting the title nodes and adding them
// to the new arraylist 'filteredTitles'
foreach(string node in titles) {
if (node == titleNode) {
filteredTitles.add(node)
}
}
// apply the filtered titles to the arraylist
listView = (ListView) findViewById(R.id.your_list_view_id);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
filteredTitles );
listView.setAdapter(arrayAdapter);
您可以这样做:
在你的活动课上
private String[] items;
在onCreate方法中,初始化项数组:
items = getResources().getStringArray(R.array.<your-string-array-name>);
并将此项数组与适配器一起使用。此问题可能会被搁置,因为您没有提供XML文件,也没有提供预期结果的外观/行为。这里的否决票只是表明需要紧急关注。