实现多级树形列表问题

阿顺顺 发布于 2013/04/14 12:15
阅读 478
收藏 0

@迷途d书童 你好,想跟你请教个问题:你好,冒昧请教问题,见谅!我在做一个展示部门及用户的列表,我获取到的数据源是XML,

<?xml version="1.0" encoding="UTF-8"?>
<pubgrouplist>
  <pubgroup id="1974" name="QA测试部">
    <user avatarlocation="http://192.168.0.38:80/avatar/2927/4aff900d-0baa-4c5c-9e72-7d6d275e5008.png" avatarname="4aff900d-0baa-4c5c-9e72-7d6d275e5008.png" bsystemavatar="0" commentname="" id="2927" needauth="0" nickname="puio" privacy="0" sign="" usercode="0" />
    <user avatarlocation="" avatarname="" bsystemavatar="0" commentname="" id="0" needauth="0" nickname="" privacy="0" sign="" usercode="0" />
    <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="112207" needauth="0" nickname="dfhyjg" privacy="0" sign="" usercode="0" />
    <pubgroup id="11691" name="神州数码">
      <user avatarlocation="http://192.168.0.38:80/avatar/1022/AVATAR_1022/96e5088a-efa3-4d10-9197-559976fff706.png" avatarname="96e5088a-efa3-4d10-9197-559976fff706.png" bsystemavatar="0" commentname="" id="1022" needauth="1" nickname="张" privacy="0" sign="测试" usercode="0" />
    </pubgroup>
    <pubgroup id="11733" name="神州数码1">
      <user avatarlocation="http://192.168.0.38:80/avatar/1023/AVATAR_1023/643ea9b3-4a8c-45df-a13b-c6a96227fc95.png" avatarname="643ea9b3-4a8c-45df-a13b-c6a96227fc95.png" bsystemavatar="0" commentname="" id="1023" needauth="1" nickname="yuiytu" privacy="0" sign="88888" usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="111815" needauth="0" nickname="dsgr" privacy="0" sign="" usercode="0" />
      <pubgroup id="11734" name="ssssssss22vddddf">
        <user avatarlocation="http://192.168.0.70:80/avatar/2906/AVATAR_2906/1b601233-d8a5-45da-beb8-e555aafb37d5.png" avatarname="1b601233-d8a5-45da-beb8-e555aafb37d5.png" bsystemavatar="0" commentname="" id="2906" needauth="1" nickname="rtyrtu" privacy="0" sign="123456dsafdsafa" usercode="0" />
        <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="111908" needauth="0" nickname="sdfhgd" privacy="0" sign="" usercode="0" />
      </pubgroup>
    </pubgroup>
    <pubgroup id="11740" name="我的歌声里">
      <user avatarlocation="http://192.168.0.152:80/avatar/1015/746bccb9-ea64-407e-b730-45d62a2994c3.png" avatarname="746bccb9-ea64-407e-b730-45d62a2994c3.png" bsystemavatar="0" commentname="" id="1015" needauth="0" nickname="erqwer" privacy="0" sign="aa" usercode="0" />
    </pubgroup>
    <pubgroup id="11759" name="神州数码2">
      <user avatarlocation="http://192.168.0.70:80/avatar/1027/AVATAR_1027/e975aed5-108a-4344-b43e-a46a561435c9.png" avatarname="e975aed5-108a-4344-b43e-a46a561435c9.png" bsystemavatar="0" commentname="" id="1027" needauth="1" nickname="puiop" privacy="0" sign="YTRUTRUYRUYRU              " usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="111903" needauth="0" nickname="ghjk" privacy="0" sign="" usercode="0" />
    </pubgroup>
  </pubgroup>
  <pubgroup id="1985" name="公司管理层">
    <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="885" needauth="0" nickname="无非" privacy="0" sign="" usercode="0" />
    <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="886" needauth="0" nickname="法国" privacy="0" sign="" usercode="0" />
    <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="887" needauth="0" nickname="个人" privacy="0" sign="" usercode="0" />
    <user avatarlocation="http://192.168.0.152:80/avatar/1015/746bccb9-ea64-407e-b730-45d62a2994c3.png" avatarname="746bccb9-ea64-407e-b730-45d62a2994c3.png" bsystemavatar="0" commentname="" id="1015" needauth="0" nickname="tyeuy" privacy="0" sign="aa" usercode="0" />
  </pubgroup>
  <pubgroup id="1986" name="销售中心">
    <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="886" needauth="0" nickname="怪物史瑞克" privacy="0" sign="" usercode="0" />
    <user avatarlocation="http://192.168.0.70:80/avatar/1027/AVATAR_1027/e975aed5-108a-4344-b43e-a46a561435c9.png" avatarname="e975aed5-108a-4344-b43e-a46a561435c9.png" bsystemavatar="0" commentname="" id="1027" needauth="1" nickname="wryttr" privacy="0" sign="YTR" usercode="0" />
    <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="111897" needauth="0" nickname="飞" privacy="0" sign="" usercode="0" />
    <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="112198" needauth="0" nickname="如一" privacy="0" sign="" usercode="0" />
    <pubgroup id="1996" name="西安办事处">
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="954" needauth="0" nickname="袁方方" privacy="0" sign="" usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="955" needauth="0" nickname="薛让春" privacy="0" sign="" usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="956" needauth="0" nickname="左5r" privacy="0" sign="" usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="957" needauth="0" nickname="xfdg人风" privacy="0" sign="" usercode="0" />
      <user avatarlocation="http://192.168.0.38:80/avatar/1023/AVATAR_1023/643ea9b3-4a8c-45df-a13b-c6a96227fc95.png" avatarname="643ea9b3-4a8c-45df-a13b-c6a96227fc95.png" bsystemavatar="0" commentname="" id="1023" needauth="1" nickname="tyt" privacy="0" sign="88888888888888888888888888888888888888888888888888" usercode="0" />
      <user avatarlocation="http://192.168.0.70:80/avatar/1027/AVATAR_1027/e975aed5-108a-4344-b43e-a46a561435c9.png" avatarname="e975aed5-108a-4344-b43e-a46a561435c9.png" bsystemavatar="0" commentname="" id="1027" needauth="1" nickname="ret5t" privacy="0" sign="YTRUTYTUYRUYRUYRUYRUYRUYRU              " usercode="0" />
    </pubgroup>
    <pubgroup id="1997" name="沈阳办事处">
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="959" needauth="0" nickname="于日峰" privacy="0" sign="" usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="960" needauth="0" nickname="染色法" privacy="0" sign="总饿呢。。。。。。" usercode="0" />
    </pubgroup>
    <pubgroup id="1998" name="济南办事处">
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="917" needauth="0" nickname="顺丰" privacy="0" sign="呵呵呵呵呵呵呵呵" usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="963" needauth="0" nickname="李让" privacy="0" sign="感谢支持!" usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="964" needauth="0" nickname="用5让他" privacy="0" sign="" usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="965" needauth="0" nickname="高 让" privacy="0" sign="" usercode="0" />
    </pubgroup>
    <pubgroup id="1999" name="郑州办事处">
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="961" needauth="1" nickname="李天平" privacy="1" sign="前面有车 后面没辙" usercode="0" />
      <user avatarlocation="" avatarname="" bsystemavatar="1" commentname="" id="962" needauth="0" nickname="宋听冲" privacy="0" sign="" usercode="0" />
      <pubgroup id="11725" name="ghf" />
    </pubgroup>
  </pubgroup>
</pubgrouplist>
解析之后数据是这样的:

xpath: /pubgrouplist/pubgroup, value: , Attribute: id = 1974   name = QA测试部   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.38:80/avatar/2927/4aff900d-0baa-4c5c-9e72-7d6d275e5008.png   avatarname = 4aff900d-0baa-4c5c-9e72-7d6d275e5008.png   bsystemavatar = 0   commentname =    id = 2927   needauth = 0   nickname = 荣获   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 0   commentname =    id = 0   needauth = 0   nickname =    privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 112207   needauth = 0   nickname = 李文   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup, value: , Attribute: id = 11691   name = 神州数码   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.38:80/avatar/1022/AVATAR_1022/96e5088a-efa3-4d10-9197-559976fff706.png   avatarname = 96e5088a-efa3-4d10-9197-559976fff706.png   bsystemavatar = 0   commentname =    id = 1022   needauth = 1   nickname = 张   privacy = 0   sign = 公网测试   usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup, value: , Attribute: id = 11733   name = 神州数码1   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.38:80/avatar/1023/AVATAR_1023/643ea9b3-4a8c-45df-a13b-c6a96227fc95.png   avatarname = 643ea9b3-4a8c-45df-a13b-c6a96227fc95.png   bsystemavatar = 0   commentname =    id = 1023   needauth = 1   nickname = 舒   privacy = 0   sign = 88888888888888888888888888888888888888888888888888   usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 111815   needauth = 0   nickname = 荣获   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/pubgroup, value: , Attribute: id = 11734   name = ssssssss22vddddf   
xpath: /pubgrouplist/pubgroup/pubgroup/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.70:80/avatar/2906/AVATAR_2906/1b601233-d8a5-45da-beb8-e555aafb37d5.png   avatarname = 1b601233-d8a5-45da-beb8-e555aafb37d5.png   bsystemavatar = 0   commentname =    id = 2906   needauth = 1   nickname = 林   privacy = 0   sign = 123456dsafdsafa   usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 111908   needauth = 0   nickname = 林嗄   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup, value: , Attribute: id = 11740   name = 我的歌声里   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.152:80/avatar/1015/746bccb9-ea64-407e-b730-45d62a2994c3.png   avatarname = 746bccb9-ea64-407e-b730-45d62a2994c3.png   bsystemavatar = 0   commentname =    id = 1015   needauth = 0   nickname = 肖   privacy = 0   sign = aa   usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup, value: , Attribute: id = 11759   name = 神州数码2   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.70:80/avatar/1027/AVATAR_1027/e975aed5-108a-4344-b43e-a46a561435c9.png   avatarname = e975aed5-108a-4344-b43e-a46a561435c9.png   bsystemavatar = 0   commentname =    id = 1027   needauth = 1   nickname = 刘   privacy = 0   sign = YTRUTYTUYRUYRUYRUYRUYRUYRU                 usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 111903   needauth = 0   nickname = 刘给   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup, value: , Attribute: id = 1985   name = 公司管理层   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 885   needauth = 0   nickname = 无非   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 886   needauth = 0   nickname = 法国   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 887   needauth = 0   nickname = 个人   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.152:80/avatar/1015/746bccb9-ea64-407e-b730-45d62a2994c3.png   avatarname = 746bccb9-ea64-407e-b730-45d62a2994c3.png   bsystemavatar = 0   commentname =    id = 1015   needauth = 0   nickname = 肖   privacy = 0   sign = aa   usercode = 0   
xpath: /pubgrouplist/pubgroup, value: , Attribute: id = 1986   name = 销售中心   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 886   needauth = 0   nickname = 怪物史瑞克   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.70:80/avatar/1027/AVATAR_1027/e975aed5-108a-4344-b43e-a46a561435c9.png   avatarname = e975aed5-108a-4344-b43e-a46a561435c9.png   bsystemavatar = 0   commentname =    id = 1027   needauth = 1   nickname = 刘   privacy = 0   sign = YTRUTYTUYRUYRUYRUYRUYRUYRU                 usercode = 0   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 111897   needauth = 0   nickname = 飞   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 112198   needauth = 0   nickname = 如一   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup, value: , Attribute: id = 1996   name = 西安办事处   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 954   needauth = 0   nickname = 袁方方   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 955   needauth = 0   nickname = 薛让春   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 956   needauth = 0   nickname = 左5r   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 957   needauth = 0   nickname = 景人风   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.38:80/avatar/1023/AVATAR_1023/643ea9b3-4a8c-45df-a13b-c6a96227fc95.png   avatarname = 643ea9b3-4a8c-45df-a13b-c6a96227fc95.png   bsystemavatar = 0   commentname =    id = 1023   needauth = 1   nickname = 舒   privacy = 0   sign = 88888888888888888888888888888888888888888888888888   usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation = http://192.168.0.70:80/avatar/1027/AVATAR_1027/e975aed5-108a-4344-b43e-a46a561435c9.png   avatarname = e975aed5-108a-4344-b43e-a46a561435c9.png   bsystemavatar = 0   commentname =    id = 1027   needauth = 1   nickname = ��   privacy = 0   sign = YTRUTYTUYRUYRUYRUYRUYRUYRU                 usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup, value: , Attribute: id = 1997   name = 沈阳办事处   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 959   needauth = 0   nickname = 于日峰   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 960   needauth = 0   nickname = 染色法   privacy = 0   sign = 总饿呢。。。。。。   usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup, value: , Attribute: id = 1998   name = 济南办事处   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 917   needauth = 0   nickname = 顺丰   privacy = 0   sign = 呵呵呵呵呵呵呵呵   usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 963   needauth = 0   nickname = 李让   privacy = 0   sign = 感谢支持!   usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 964   needauth = 0   nickname = 用5让他   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 965   needauth = 0   nickname = 高 让   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup, value: , Attribute: id = 1999   name = 郑州办事处   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 961   needauth = 1   nickname = 李天平   privacy = 1   sign = 前面有车 后面没辙   usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/user, value: , Attribute: avatarlocation =    avatarname =    bsystemavatar = 1   commentname =    id = 962   needauth = 0   nickname = 宋听冲   privacy = 0   sign =    usercode = 0   
xpath: /pubgrouplist/pubgroup/pubgroup/pubgroup, value: , Attribute: id = 11725   name = ghf

我想实现的效果是这样的:

这个只是网上看到的一个demo的样子,我想实现的效果跟这个是一样的。我的问题是:不知道如何将数据与demo需要的数据匹配,显示树。

demo的生成树的代码是直接new的Node节点,一个一个的添加的

Node root = new Node("根节点","000000");
		root.setIcon(R.drawable.icon_department);//设置图标
		//root.setCheckBox(false);//设置节点前有无复选框  
		
		
		// 创建1级子节点
		Node n1 = new Node("子节点","1");
		n1.setParent(root);//设置父节点
		n1.setIcon(R.drawable.icon_department);
		
		Node n11 = new Node("子节点","13966664567");
		n11.setParent(n1);
		n11.setIcon(R.drawable.user_16);
		Node n12 = new Node("子节点","13966664567");
		n12.setParent(n1);
		n12.setIcon(R.drawable.user_16);
		
		n1.addNode(n11);
		n1.addNode(n12);
		
				
		// 创建1级子节点
		Node n2 = new Node("子节点","2");
		n2.setParent(root);
		n2.setIcon(R.drawable.icon_department);
		Node n21 = new Node("子节点","0");
		n21.setParent(n2);
		n21.setIcon(R.drawable.user_16);
		Node n22 = new Node("子节点","0");
		n22.setParent(n2);
		n22.setIcon(R.drawable.user_16);
		Node n23 = new Node("子节点","0");
		n23.setParent(n2);
		n23.setIcon(R.drawable.user_16);
		n2.addNode(n21);
		n2.addNode(n22);
		n2.addNode(n23);
		
		
		// 创建1级子节点
		Node n3 = new Node("子节点","0");
		n3.setParent(root);
		n3.setIcon(R.drawable.icon_department);
		Node n31 = new Node("子节点","0");
		n31.setParent(n3);
		n31.setIcon(R.drawable.user_16);
		Node n32 = new Node("子节点","0");
		n32.setParent(n3);
		n32.setIcon(R.drawable.user_16);
		
		//创建2级子节点
		Node n33 = new Node("子节点","0");
		n33.setParent(n3);
		n33.setIcon(R.drawable.icon_department);
		
		Node n331 = new Node("子节点","0");
		n331.setParent(n33);
		//n331.setIcon(R.drawable.user_16);
		Node n332 = new Node("子节点","0");
		n332.setParent(n33);
		n332.setIcon(R.drawable.user_16);
		Node n333 = new Node("子节点","0");
		n333.setParent(n33);
		//n333.setIcon(R.drawable.user_16);
		n33.addNode(n331);
		n33.addNode(n332);
		n33.addNode(n333);
		
		n3.addNode(n31);
		n3.addNode(n32);
		n3.addNode(n33);
		
		
		
		root.addNode(n3);
		root.addNode(n1);
		root.addNode(n2);
		
		
		TreeAdapter ta = new TreeAdapter(tda,root);
		// 设置整个树是否显示复选框
		ta.setCheckBox(true);
	    // 设置展开和折叠时图标
		ta.setCollapseAndExpandIcon(R.drawable.tree_ex, R.drawable.tree_ec);
		// 设置默认展开级别
		ta.setExpandLevel(2);
		listView.setAdapter(ta);
不知道能不能帮我看一下,十分感谢!没法直接联系你,如果有问题,请加我Q:914575565,谢谢!

加载中
返回顶部
顶部