android中树形json解析为对象,并通过dialog显示,多级列表
来源:互联网 发布:蓝巨星软件 编辑:程序博客网 时间:2024/06/06 16:26
{"id":"0","text":"信件简历夹","type":"root","data":"0","state":{"selected":true,"opened":true},"children":[{"id":"525B0D60762311E58D60F73FF458773A","text":"New node","data":"525B0D60762311E58D60F73FF458773A","pid":"0","state":{"selected":false,"opened":true},"children":[{"id":"525BD0B0762311E590B0C6DB849AE720","text":"New node","data":"525BD0B0762311E590B0C6DB849AE720","pid":"525B0D60762311E58D60F73FF458773A","state":{"selected":false,"opened":true},"type":"default"},{"id":"5272B410762311E5B4108E983A64A6A9","text":"New node1","data":"5272B410762311E5B4108E983A64A6A9","pid":"525B0D60762311E58D60F73FF458773A","state":{"selected":false,"opened":true},"children":[{"id":"5272DB20762311E59B20C18DB31F394D","text":"New node","data":"5272DB20762311E59B20C18DB31F394D","pid":"5272B410762311E5B4108E983A64A6A9","state":{"selected":false,"opened":true},"children":[{"id":"52735050762311E590508BDCE6B97C58","text":"New node3","data":"52735050762311E590508BDCE6B97C58","pid":"5272DB20762311E59B20C18DB31F394D","state":{"selected":false,"opened":true},"type":"default"}]},{"id":"52732940762311E5A9409FDA0F0C912E","text":"New node2","data":"52732940762311E5A9409FDA0F0C912E","pid":"5272B410762311E5B4108E983A64A6A9","state":{"selected":false,"opened":true},"type":"default"}]}]}]}
上边是从服务器获取到的json数据,是树形,并且服务器端要求json树不是固定的
做的是个菜单,比如(美食–火锅–王婆大虾)(旅游–龙门石窟)
也就是说下边的项不是固定的,可能2项,也可能3,4项
(部分示例)
{ "id": "0", "text": "信件简历夹", "type": "root", "data": "0", "state": { "selected": true, "opened": true }, "children": [ { "id": "525B0D60762311E58D60F73FF458773A", "text": "New node", "data": "525B0D60762311E58D60F73FF458773A", "pid": "0", "state": { "selected": false, "opened": true }, "children": [.........................
这是 JsonBean.java
public class JsonBean implements Serializable{ @TreeNodeId private String id;@TreeNodeLabel private String text;public String type;//判断节点的,是否跟节点或者是否有下一节点@TreeNodePid private String pid;public String data;// public JSONObject state;public JsonBean[] children;public JsonBean(String id,String pid,String text){ super();this.id = id; this.pid = pid;this.text = text; }}
下边是在Activity中的解析
//Stre为json字符串,得到data树 public void jsonDo(String Stre) {JSONObject json; try { json = new JSONObject(Stre); //json数据有一个type,当他的值不是default,则ta有children if (!"default".equals(json.getString("type"))) {// 这个时候才有children Gson gson = new Gson(); List<JsonBean> lists = gson.fromJson(json.getString("children"),new TypeToken<List<JsonBean>>() { }.getType()); System.out.println(lists.size()); arr.clear(); getDataFromListBean(lists); System.out.println("给之前 "+arr.size()); // 其中,state还需要用json解析 Intent intent=new Intent(context,ExpandDialog.class); Bundle bundle=new Bundle(); bundle.putSerializable("list",arr); intent.putExtras(bundle); context.startActivity(intent); }} catch (JSONException e) { e.printStackTrace();} } ArrayList<JsonBean> arr = new ArrayList<JsonBean>();ArrayList<JsonBean> arr1; private void getDataFromListBean(List<JsonBean> lists) { for (JsonBean jsonBean : lists) { //将遍历到的jsonBean保存到list中 arr.add(jsonBean); if (!"default".equals(jsonBean.type)) { arr1 = new ArrayList<JsonBean>(); JsonBean[] jsonArr = jsonBean.children; for (int i = 0; i < jsonArr.length; i++) { arr1.add(jsonArr[i]); }//递归 getDataFromListBean(arr1); } } }代码排版问题,看着不是很舒服,大家可以拷到软件里边看
上面是将获取到的json数据,转化为6个jsonBean对象,然后将list传给dialog(我继承的是activity,用的是官方的dialog样式)
还有就是用intent传list集合
由于菜单列是不固定的,所以用不了expandableListview
**http://blog.csdn.net/lmj623565791/article/details/40212367**借用大牛的无限树,具体dialog用法就请大家参考大牛写的
public class ExpandDialog extends BaseActivity
{
private List mylist = new ArrayList();
private ListView mTree;
private TreeListViewAdapter mAdapter;
private String pageClick=”0”;
private String selectUrl=”“;
private Bundle b;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.expand_dialog);
b = getIntent().getExtras();
mTree = (ListView) findViewById(R.id.id_tree);
mylist=(List) b.getSerializable(“datalist”);
if(mylist==null){
initDate();
}
try
{
mAdapter = new SimpleTreeAdapter(mTree, this, mylist, 10);
mAdapter.setOnTreeNodeClickListener(new OnTreeNodeClickListener()
{
@Override
public void onClick(Node node, int position)
{
if (node.isLeaf())
{
Toast.makeText(getApplicationContext(), node.getText(),Toast.LENGTH_SHORT).show(); String id=node.getId(); String pid=node.getPid(); String text=node.getText(); JsonBean bean=new JsonBean(id, pid, text); Intent data = getIntent(); Bundle b = new Bundle(); b.putSerializable("bean",bean); data.putExtras(b); setResult(88, data); finish(); } } }); } catch (Exception e) { e.printStackTrace(); } mylist.clear(); mTree.setAdapter(mAdapter);}
“`
因为是在项目中,也没时间把它摘出来,多多包涵
- android中树形json解析为对象,并通过dialog显示,多级列表
- android中树形json解析为对象,并通过dialog显示,多级列表
- android多级树形列表
- Android 使用GSON解析JSON 并转换为JAVA对象
- mfc 遍历ftp服务器文件并显示树形列表中
- extjs多级树形列表——json传参
- 前端小结--ajax和spring互传json对象,并显示到列表中
- Android 中json 对象解析
- 用fastjson为Ztree生成多级树形菜单JSON数据源
- android好友多级列表显示Expandabl…
- springmvc通过@ResponseBody 自动解析为json对象支持中文
- Android多级树形结构列表(理论上可以无限级)
- 更快实现Android多级树形选择列表
- Android中通过Gson进行json解析
- JavaScript加载XML文件,并解析为JSON对象
- Android学习 (十八) 用GSON解析JSON数据并在ListView中显示
- 解析Json数据并通过Fragment填充到ViewPager中
- android中使用httpclient方法获得网页内容并对json对象解析
- 无法打开keneral.lib 亲身经历,找了很久,就这个有用
- oracle更改字符集AL32UTF8为ZHS16GBK
- Zookeeper基本原理
- MyEclipse开发Web项目发布到Tomcat下的Root目录
- pip装了一个包,但是python里Import的时候找不到怎么办?
- android中树形json解析为对象,并通过dialog显示,多级列表
- LDA理解以及源码分析(二)
- oc中null NULL nil void (void*)实例讲解
- final
- java获取当前ip
- 双端链表
- 如何给UIViewController瘦身
- zzulioj 1803: SC晾衣服 (暴力+技巧转换)
- HttpClient 4.3教程