解析XML数据到哈希表中并显示在列表视图中?
问题描述:
我有一个问题。我有多个记录,SAX从需要插入到Hashtable并以列表视图显示的xml文件中解析出来。我能够在列表视图中显示数据,但它给出的是与列表视图中所有行相同的数据。我认为这与编码散列表或我的SAX解析有关。解析XML数据到哈希表中并显示在列表视图中?
这是XML文件,我试图从网上检索数据:http://spark.opac.tp.edu.sg/X?op=present&set_no=007584&set_entry=000000001,000000002,000000003,000000004,000000005&format=marc
下面是我的处理程序在那里我将我的数据放到哈希表:
任何建议家伙?
答
我没有时间来完全地读你的代码...
但是,这是一个简单的例子:
ListView myListView;
myListView = (ListView) findViewById(R.id.mylistview);
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
//load your data
String[][] items = database.getItems("Blog");
//check if the database was empty
if(items != null){
for(int i = 0; i < items[0].length; i++) {
map = new HashMap<String, String>();
map.put("pubdate", items[2][0]);
map.put("title", items[0][i]);
map.put("description", items[1][i]);
listItem.add(map);
}
//Creation of a SimpleAdapter to put items in your list (listitems) in your listview
// You need to have a xml file called list_full_news_item with elements
// called android:id="@+id/title" etc.
SimpleAdapter mSimpleAdaptor = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list_full_news_item,
new String[] {"pubdate", "title", "description"}, new int[] {R.id.itemPubdate, R.id.itemTitle, R.id.itemDescription});
//Assign to the listView the created adapter
myListView.setAdapter(mSimpleAdaptor);
}
希望这将有助于你理解。
答
您不应使用SAX解析器,而应使用Simple XML Library。它具有@ElementMap annotation正是为了这个目的。如果你想看看如何使用它的所有酷炫功能,这将把你的整个问题变成一个注释和三行代码来读取XML中的文件。Look at my blog post关于在android项目中包含库以及look at the Simple tutorial。
编辑:我刚刚意识到我已经在你的previous questions之前回答过这个问题。我试图说服你看看简单的XML;但请在这里以五种不同的方式停止发布基本上完全相同的问题;人们已经帮助过你,给你答案并带领你走向正确的方向。现在是阅读更多,实验和学习的时候了。
谢谢^^。为我完美工作 – 2011-05-05 03:29:04