从xml列出视图

时间:2012-12-21 06:49:28

标签: android xml listview

请参阅this image,我想以此表格显示我的短信,请帮助我

这是xml文件:

<smses count="2">

<sms 
    protocol="0" 
    address="123" 
    date="1356069180000" 
    type="1" 
    subject="null" 
    body="nDGd0Or7LQCajC+GSkQFDw==" 
    toa="null" 
    sc_toa="null" 
    service_center="null" 
    read="0" 
    status="0" 
    locked="0" 
    date_sent="0" 
    readable_date="Dec 21, 2012 5:53:00 AM" 
    contact_name="(Unknown)"/>

<sms 
    protocol="0" 
    address="123" 
    date="1356069181021" 
    type="1" 
    subject="null" 
    body="hw r u" 
    toa="null" 
    sc_toa="null" 
    service_center="null" 
    read="0" 
    status="-1" 
    locked="0" 
    date_sent="1356069180000" 
    readable_date="Dec 21, 2012 5:53:01 AM" 
    contact_name="(Unknown)"/>
</smses>    

try{    
    File fXmlFile = new File(/mnt/sdcard/myxmlfile.xml);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
    NodeList nl = doc.getElementsByTagName("sms");
    pDialog.setMax(nl.getLength());
    for (int i = 0; i < nl.getLength(); i++) {

        HashMap<String, String> map = new HashMap<String, String>();
        Node nNode = nl.item(i);
        Element e = (Element) nNode;
        String number= getNodeAttributeByTagName(nNode, "address");
        map.put(KEY_ADDRESS,number);
        map.put("body",getNodeAttributeByTagName(nNode, "body"));
        map.put(KEY_CONTACT, getContactDisplayNameByNumber(number));
        menuItems.add(map);
        String y = String.valueOf(i);
        publishProgress(y);
    }
  }catch(Exception e){
    e.printStackTrace();
}

在列表中显示

ListAdapter adapter = new SimpleAdapter(showlist.this, menuItems,
                R.layout.list_item_albums,
                new String[] { KEY_CONTACT, "body", KEY_ADDRESS }, new int[] {
                R.id.contact, R.id.body, R.id.address  });
        list1.setAdapter(adapter);
    }

1 个答案:

答案 0 :(得分:0)

@rohit:你必须为每一行创建一个自定义视图。

请参阅此ListView教程。