DOM解析XML文件2-SimpleAdapter_simple_list_item_2布局

来源:互联网 发布:网络安全技术高峰论坛 编辑:程序博客网 时间:2024/06/07 13:18

1.新建名为domxmla的Android Application Project
这里写图片描述
2.User.java

package com.example.domain;public class User {    private Integer id;    private String name;    public User() {        super();        // TODO Auto-generated constructor stub    }    public User(Integer id, String name) {        super();        this.id = id;        this.name = name;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "User [id=" + id + ", name=" + name + "]";    }}

3.users.xml

<?xml version="1.0" encoding="UTF-8"?><users>    <user>        <id>1</id>        <name>chj</name>        <phone>13521768691</phone>    </user>    <user>        <id>2</id>        <name>bc</name>         <phone>13521768692</phone>    </user>    <user>        <id>3</id>        <name>wmm</name>         <phone>13521768693</phone>    </user>    <user>        <id>4</id>        <name>yxh</name>         <phone>13521768694</phone>    </user>    <user>        <id>5</id>        <name>hl</name>         <phone>13521768697</phone>    </user>    <user>        <id>1</id>        <name>chj</name>        <phone>13521768697</phone>    </user>    <user>        <id>2</id>        <name>bc</name>         <phone>13521768697</phone>    </user>    <user>        <id>3</id>        <name>wmm</name>         <phone>13521768697</phone>    </user>    <user>        <id>4</id>        <name>yxh</name>         <phone>13521768697</phone>    </user>    <user>        <id>5</id>        <name>hl</name>         <phone>13521768697</phone>    </user></users>

4.MainActivity.java
注意:这里采用的是ListView的simple_list_item_2布局

package com.example.domxml;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;public class MainActivity extends Activity {    // 声明控件对象 视图V    private ListView lv_users;    // 集合    private List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 获取控件对象        lv_users = (ListView) findViewById(R.id.lv_users); // 视图 jsp html        try {            // dom解析 users.xml asserts目录中            domParser();            /*             * context 上下文 data list集合 resource 资源布局文件             * android.R.layout.simple_list_item_2 text1 text2             *              * from map集合中的key是String ids names             *              * to ids显示在布局文件中对应text2 names显示对应text1             */            // 适配器 控制层 C            ListAdapter adapter = new SimpleAdapter(this, data,                    android.R.layout.simple_list_item_2, new String[] {                            "names", "phone" }, new int[] { android.R.id.text1,                            android.R.id.text2 });            // 设置适配器            lv_users.setAdapter(adapter);            // 思考 点击具体的一个条目            lv_users.setOnItemClickListener(new OnItemClickListener() {                @Override                public void onItemClick(AdapterView<?> parent, View view,                        int position, long id) {                    //获取view控件中的控件  包括用户名控件 电话的控件                    TextView tv_name = (TextView) view.findViewById(android.R.id.text1);                    TextView tv_phone = (TextView) view.findViewById(android.R.id.text2);                    //获取用户名称                    String name = tv_name.getText().toString();                    //获取用户电话                    String phone = tv_phone.getText().toString();                    //执行打电话                    //1.定义打电话的意图对象                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone));                    //2.执行意图                    startActivity(intent);                    //3.记得添加打电话的权限  在项目清单中添加                }            });        } catch (ParserConfigurationException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SAXException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    /**     * 解析方法     *      * @throws ParserConfigurationException     * @throws IOException     * @throws SAXException     */    private void domParser() throws ParserConfigurationException, IOException,            SAXException {        // 1.创建DocumentBuilderFactory对象        DocumentBuilderFactory builderFactory = DocumentBuilderFactory                .newInstance();        // 2.通过工厂对象获取DocumentBuilder        DocumentBuilder builder = builderFactory.newDocumentBuilder();        //        // 3 获取要解析的xml文件 对应的输入流        InputStream is = getAssets().open("users.xml");        // 4.利用DocumentBuilder的parse方法获取解析的users.xml的document对象        Document document = builder.parse(is);        // 5.处理解析        // 1.获取文档的跟节点 users        Element root = document.getDocumentElement();        // 2.获取当前根节点下面所有user节点的集合        NodeList nodeList = root.getElementsByTagName("user");        // 遍历孩子节点        for (int i = 0; i < nodeList.getLength(); i++) {            // 获取具体的孩子节点            Node node = nodeList.item(i); // user            // 创建map对象            Map<String, Object> map = new HashMap<String, Object>();            // 获取user节点的所有孩子节点 id name            NodeList nlist = node.getChildNodes();            // 遍历孩子节点            for (int index = 0; index < nlist.getLength(); index++) {                // 获取具体的孩子节点                Node n = nlist.item(index);                // 判断该节点的名称是否是id                if (n.getNodeName().equals("id")) {                    // 设置user的id值                    map.put("ids", n.getTextContent());                    // 判断该节点的名称是否是name                } else if (n.getNodeName().equals("name")) {                    // 设置name值                    map.put("names", n.getTextContent());                } else if (n.getNodeName().equals("phone")) {                    map.put("phone", n.getTextContent());                }            }            // 添加到集合中            data.add(map);        }    }}

5.在activity_main.xml中添加ListView控件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <ListView        android:id="@+id/lv_users"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>

这里写图片描述
6.显示效果
这里写图片描述

3 0
原创粉丝点击