显示链表时出现问题



我在显示字符串列表时遇到问题。我在链表中解析了 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;
}
 }

嗯......你的安卓手机里有应用服务器吗?或者此 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 活动框架不使用无参数构造函数来实例化它。将该代码移动到您的onCreate()

相关内容

  • 没有找到相关文章

最新更新