-1

我想通过解析 XML 创建一个应用程序员工详细信息。其中将显示 empid、empname、empcost 等员工详细信息。我想在从 xml 获取一些数据后立即显示可绘制文件夹中的图像。然后我需要当我点击每张图片时,它会显示员工的详细信息:empid、empname、empcost 在另一个活动中。以下是我的 java 代码,它显示了从 ListView 中的 XML 解析的数据:

爪哇:

public class XmlVdUI extends ListActivity {

    Document doc;

    private Element getChildEle(Element parentEle, String common) {

        for (Node n = parentEle.getFirstChild(); n != null; n = n
                .getNextSibling()) {
            if (n.getNodeType() == Node.ELEMENT_NODE
                    && n.getNodeName().equals(common)) {
                return (Element) n;
            }
        }
        return null;
    }

    public static Iterable<Element> getChildren(Element common, String group) {
        List list = new ArrayList();

        for (Node n = common.getFirstChild(); n != null; n = n.getNextSibling()) {
            if (n.getNodeType() == Node.ELEMENT_NODE
                    && n.getNodeName().equals(group)) {
                list.add(n);
            }
        }
        return (Iterable<Element>) list;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        parseXml();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xml_vd_ui);

        final String _EMPID = "Empid";
        final String _EMPNAME = "Name";
        final String _EMPCOST = "Empcost";

        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        Element commonEle = getChildEle(doc.getDocumentElement(), "Common");

        Iterable<Element> groupEle = (Iterable<Element>) getChildren(commonEle,
                "Group"); // Iterable<Element>
        groupEle = (Iterable<Element>) getChildren(commonEle, "Group");
        System.out.println("hi");

        for (Element e : groupEle) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(_EMPID, getValue(e, _EMPID));
            map.put(_EMPNAME, getValue(e, _EMPNAME));
            map.put(_EMPCOST, "Rs." + getValue(e, _EMPCOST));

            // adding HashList to ArrayList menuItems.add(map);
            menuItems.add(map);
        }
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { _EMPNAME, _EMPID, _EMPCOST }, new int[] {
                        R.id.name, R.id.id, R.id.cost });

        setListAdapter(adapter);

    }

    private String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);

        return this.getElementValue(n.item(0));
    }

    public final String getElementValue(Node ele) {
        Node child;
        if (ele != null) {
            if (ele.hasChildNodes()) {
                for (child = ele.getFirstChild(); child != null; child = child
                        .getNextSibling()) {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        return child.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

    public void parseXml() {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputStream in = getResources().openRawResource(R.raw.root1);
            doc = db.parse(in, null);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

一旦我从 xml 而不是在 ListView 中获取一些数据,我就需要从可绘制文件夹中显示图像。

4

1 回答 1

0

听起来你想为你的 ImageViews 设置一个onClickListener,它将启动带有员工信息的第二个 Activity。

如果您还有其他问题,我可以扩展我的答案。

于 2013-02-08T18:39:28.143 回答