2013-02-02 66 views
0

我必須開發一個android應用程序。getattribute在Android的listview與hashmap

其執行的屬性值是在列表視圖顯示...

這是我的XML標籤:

<root> 
<Categories> 
<Category name="books"> 
<Articles> 
    <article articleid="170" title="Colour And Vibrancy Are Part Of Our DNA"> 
    <Description> 
    Well-known interior designer 
</Description> 

我用下面的代碼:

public class MainActivity extends Activity { 

    String URL; 
    static String KEY_CATEGORY = "Categories"; 
    ArrayList<HashMap<String, String>> songsList; 
    static final String KEY_PNAME = "Category"; 
    static final String KEY_PRICE = "Description"; 
    static final String KEY_THUMB_URL = "thumb_image"; 

     ListAdapter adapter; 

    /** Called when the activity is first created. */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     songsList = new ArrayList<HashMap<String, String>>(); 
    HorizontalListView list = (HorizontalListView) findViewById(R.id.listView1); 

    adapter = new ListAdapter(this, songsList); 
    list.setAdapter(adapter); 

    URL="http://webservices/new_feed_articls.xml"; 

     XMLParser parser = new XMLParser(); 
     String xml = parser.getXmlFromUrl(URL); // getting XML from URL 
     Document doc = parser.getDomElement(xml);// getting DOM element 

     NodeList nl = doc.getElementsByTagName(KEY_CATEGORY); 
     int numResults = parser.numResults(doc); 

     if((numResults <= 0)){ 
      Toast.makeText(MainActivity.this, "There is no data in the xml file", Toast.LENGTH_LONG).show(); 
      finish(); 
     } 


     // looping through all song nodes &lt;song&gt; 
     for (int i = 0; i < nl.getLength(); i++) { 
      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 
      Element e = (Element) nl.item(i); 
      // adding each child node to HashMap key =&gt; value 
      map.put(KEY_PNAME,((Element)nl.item(i)).getAttribute("name")); 


      map.put(KEY_PRICE, parser.getValue(e, KEY_PRICE)); 
       map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL)); 

      // adding HashList to ArrayList 
      songsList.add(map); 
     } 


     // Click event for single list row 
     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       HashMap<String, String> map = songsList.get(position); 

這裏我運行應用程序意味着需要在listview上顯示類別名稱。如何獲取屬性值?請幫幫我。 http://sogacity.com/how-to-make-a-custom-arrayadapter-for-listview/

回答

0

加載從XML數據,加載它在到songslist,它裝入適配器,並用適配器getview膨脹的佈局,並在列表視圖顯示它...一下這個例子後我得到的O/p下面的代碼使用後:

我已經做過的getElementsByTagName的錯誤...

我現在已經改變了標籤,如:

NodeList nl = doc.getElementsByTagName(KEY_PNAME); 

現在我必須運行輸出意味着獲得屬性值...