我目前正在创建一个 android 应用程序,并且我有一个供用户选择的列表,并且我希望旁边的每个项目都有一个图像。我为列表创建了一个适配器,并为列表创建了一个单独的 list_row 布局。
列表的实际数据存储在本地 res/xml/data.xml 中,如下所示:
<dataset>
<data>
<name>all</name>
<image>all</image>
</data>
<data>
<name>car</name>
<image>car</image>
</data>
</dataset>
现在,在此列表的主要活动中,我尝试从本地源解析 XML 数据,然后将其作为字符串传递给 Document 元素并处理数据。我已经调试了这个问题,并注意到下面的行不起作用,因为设置的字符串为空:
XmlResourceParser parserXml = getResources().getXml(R.xml.data);
String xmlString = parserXml.getText();
解析数据的完整代码如下:
XmlResourceParser parserXml = getResources().getXml(R.xml.dresses);
String xmlString = parserXml.getText(); // getting XML from URL
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xmlString); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_DATA);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_IMAGE, parser.getValue(e, KEY_IMAGE));
dataInfo.add(map);
}
任何人都可以帮我解决这个问题,在此先感谢:)
编辑.....
我也尝试过按照建议使用 XmlPullParser 方法,但我仍然遇到相同的问题,即 String 为空。我使用的代码如下:
XmlPullParser xpp = this.getResources().getXml(R.xml.data);
String xmlString1 = xpp.getText();
如果这有帮助,我在下面包含了整个文件:
public class Dresses extends Activity {
static final String KEY_DATA = "dress"; // parent node
static final String KEY_NAME = "name";
static final String KEY_IMAGE = "image";
ListView list;
ItemTypeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, String>> itemTypes = new ArrayList<HashMap<String, String>>();
XmlPullParserFactory factory;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
} catch (XmlPullParserException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
XmlPullParser xpp = this.getResources().getXml(R.xml.data);
String xmlString1 = xpp.getText();
Log.d("Testing Type List Data: ", xmlString1);
XmlResourceParser parserXml = getResources().getXml(R.xml.data);
String xmlString = parserXml.getText(); // getting XML from URL
Log.d("Type List Data: ", xmlString);
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xmlString); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_DATA);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_IMAGE, parser.getValue(e, KEY_IMAGE));
itemTypes.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new ItemTypeAdapter(this, itemTypes);
list.setAdapter(adapter);
// listening to single list item on click
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String type = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), ItemListActivity.class);
// sending data to new activity
i.putExtra("type", type);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.data, menu);
return true;
}
}