使用hashmap在android listview中获取getattribute

时间:2013-02-02 10:33:36

标签: java android xml-parsing hashmap getattribute

我必须开发一个Android应用程序。

它执行的属性值显示在listview ...

这是我的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上显示类别名称。如何才能获得属性值???请帮帮我。

2 个答案:

答案 0 :(得分:0)

从xml加载数据后,将其加载到songslist中,将其加载到Adapter中,然后使用Adapter getview扩展布局并将其显示在listview中...查看此示例: http://sogacity.com/how-to-make-a-custom-arrayadapter-for-listview/

答案 1 :(得分:0)

我在使用下面的代码后获得了o / p:

我在getElementsByTagName中犯了错误......

现在我更改了标签:

NodeList nl = doc.getElementsByTagName(KEY_PNAME);

现在我必须运行输出意味着我很好地获得属性值...