显示链接列表的问题

时间:2011-12-10 23:18:13

标签: java android linked-list

我在显示字符串列表时遇到问题。我在链表中​​解析了xml文件并将其转换为字符串列表。但是,当我启动模拟器时,它没有显示任何内容。这是我的代码;

 package com.smart.house;
 import java.util.LinkedList;
 import android.app.ListActivity;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;

 public class DevicesActivity extends ListActivity {
public LinkedList<Devices> getDevices;

public DevicesActivity() {
    try {
        this.getDevices = new ServiceCall().getDevices();
    } catch (Exception e) {

    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    try {

        String[] devices = new String[getDevices.size()];

        for (int i = 0; i < getDevices.size(); i++) {
            devices[i] = getDevices.get(i).getShortName();
        }

        setListAdapter(new ArrayAdapter<String>(DevicesActivity.this,
                android.R.layout.simple_list_item_1, devices));

    } catch (Exception e) {

    }
}
}

以下是解析代码:

 package com.smart.house;

 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.LinkedList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.w3c.dom.Element;

public class ServiceCall {

public LinkedList<Devices> getDevices() throws IOException, SAXException,
        ParserConfigurationException {

    LinkedList<Devices> deviceslist = new LinkedList<Devices>();
    URL url = new URL("http://127.0.0.1/android/devices.xml");
    InputStream is = url.openStream();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(is);
    doc.getDocumentElement().normalize();

    NodeList nodeLst = doc.getElementsByTagName("device");
    for (int s = 0; s < nodeLst.getLength(); s++) {
        Node fstNode = nodeLst.item(s);
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

            Element fstElmnt = (Element) fstNode;

        NodeList fstNmElmntLst =       fstElmnt.getElementsByTagName("id");
            Element fstNmElmnt = (Element) fstNmElmntLst.item(0);

            NodeList fstNm = fstNmElmnt.getChildNodes();

        long id = Long.parseLong(((Node) fstNm.item(0)).getNodeValue());

            NodeList lstNmElmntLst = fstElmnt
                    .getElementsByTagName("shortName");
            Element lstNmElmnt = (Element) lstNmElmntLst.item(0);

            NodeList lstNm = lstNmElmnt.getChildNodes();

            String shortName = ((Node) lstNm.item(0)).getNodeValue();

            NodeList lstNmElmntLst1 = fstElmnt
                    .getElementsByTagName("fullName");
            Element lstNmElmnt1 = (Element) lstNmElmntLst1.item(0);
            NodeList lstNm1 = lstNmElmnt1.getChildNodes();

            String fullName = ((Node) lstNm1.item(0)).getNodeValue();

            deviceslist.add(new Devices(id, shortName, fullName));

        }
    }
    return deviceslist;

}

}

package com.smart.house;

public class Devices {

private long id;
private String shortName;
private String fullName;

public Devices(long id, String shortName, String fullName){
    setId(id);
    setShortName(shortName);
    setFullName(fullName);
}
public void setId(long id) {
    this.id = id;
}

public long getId() {
    return id;
}

public void setShortName(String shortName) {
    this.shortName = shortName;
}

public String getShortName() {
    return shortName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public String getFullName() {
    return fullName;
}
 }

1 个答案:

答案 0 :(得分:0)

嗯...你的Android手机上有appserver吗?或者这个URL在您的实际运行代码中有所不同吗?

URL url = new URL("http://127.0.0.1/android/devices.xml");

编辑:

我想我看到了什么是错的。您在自定义构造函数中实例化您的设备:

public DevicesActivity() {
    try {
        this.getDevices = new ServiceCall().getDevices();
    } catch (Exception e) {

    }
}

但是,Android活动框架不使用no-parameters构造函数来实例化它。将该代码移至onCreate()