2011-05-04 98 views
2


我有一個問題。我有多個記錄,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

下面是我的處理程序在那裏我將我的數據放到哈希表:

​​

任何建議傢伙?

回答

1

我沒有時間來完全地讀你的代碼...

但是,這是一個簡單的例子:

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); 
} 

希望這將有助於你理解。

+0

謝謝^^。爲我完美工作 – 2011-05-05 03:29:04

1

您不應使用SAX解析器,而應使用Simple XML Library。它具有@ElementMap annotation正是爲了這個目的。如果你想看看如何使用它的所有酷炫功能,這將把你的整個問題變成一個註釋和三行代碼來讀取XML中的文件。Look at my blog post關於在an​​droid項目中包含庫以及look at the Simple tutorial

編輯:我剛剛意識到我已經在你的previous questions之前回答過這個問題。我試圖說服你看看簡單的XML;但請在這裏以五種不同的方式停止發佈基本上完全相同的問題;人們已經幫助過你,給你答案並帶領你走向正確的方向。現在是閱讀更多,實驗和學習的時候了。