【问题标题】:problems with showing a linked list显示链接列表的问题
【发布时间】:2011-12-10 23:18:12
【问题描述】:

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

【问题讨论】:

  • 您是否知道您的节点列表中是否有任何内容?您是否从循环中进行了任何日志记录以查看它在做什么?
  • 是的,它可以工作...我已经用 java 试过了,但它不适用于 android

标签: java android linked-list


【解决方案1】:

嗯...您的 Android 手机中是否运行了应用服务器?还是这个 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()

【讨论】:

  • 我已经安装了 wamp 服务器并将我的 xml 放在上面,我认为它可以工作......我认为问题出在这个 ArrayAdapter
  • 您的 setAdapter() 对我来说看起来不错。尝试记录设备名称并查看它们是否真的存在:for (int i = 0; i &lt; getDevices.size(); i++) { devices[i] = getDevices.get(i).getShortName(); Log.d("TAG", devices[i]); }
  • 实际上,您还应该删除该 try/catch,(或在遇到异常时记录一些内容),可能是错误的,您永远不会知道!
猜你喜欢
  • 2023-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 2018-01-04
  • 2020-09-09
  • 2013-11-06
  • 2014-01-23
相关资源
最近更新 更多