android中树形json解析为对象,并通过dialog显示,多级列表
来源:互联网 发布:组策略 安装软件 编辑:程序博客网 时间:2024/06/13 02:00
{"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.javapublic 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; }
}
// Stre为json字符串,得到data树 public void jsonDo(String Stre) {JSONObject json; try {json = new JSONObject(Stre); //json数据有一个type,当他的值不是default,则ta有childrenif (!"default".equals(json.getString("type"))) {// 这个时候才有childrenGson 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<JsonBean>传给dialog(我继承的是activity,用的是官方的dialog样式)
还有就是用intent传list集合
由于菜单列是不固定的,所以用不了expandableListview
http://blog.csdn.net/lmj623565791/article/details/40212367
借用大牛的无限树,具体dialog用法就请大家参考大牛写的
public class ExpandDialog extends BaseActivity{//private List<JsonBean> mDatas2 = new ArrayList<JsonBean>();private List<JsonBean> mDatas2 = new ArrayList<JsonBean>();private ListView mTree; private TreeListViewAdapter mAdapter;String pageClick="0"; String selectUrl="";ArrayList<JsonBean> mylist; @Overrideprotected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);setContentView(R.layout.expand_dialog); mTree = (ListView) findViewById(R.id.id_tree);
//获取传过来的list集合 mylist=(ArrayList<JsonBean>)getIntent().getSerializableExtra("list");//System.out.println(mylist.size()); try{ mAdapter = new SimpleTreeAdapter<JsonBean>(mTree, this, mylist, 10);mAdapter.setOnTreeNodeClickListener(new OnTreeNodeClickListener(){ @Overridepublic void onClick(Node node, int position){ if (node.isLeaf()){ String talentsReserveIds=OpenSharedPerferences.read(getApplicationContext(), "talentStore", "talentsReserveIds");pageClick=OpenSharedPerferences.read(getApplicationContext(), "talentStore", "pageClick");RequestParams params=new RequestParams(); if(pageClick.equals("1")){selectUrl=ConstantURL.REMOVE_RESUME_CLIP; params.addBodyParameter("companyId", "535662D0416911E5A076F8F061532E9D");params.addBodyParameter("favoritesIds", node.getId());params.addBodyParameter("talentsReserveIds", talentsReserveIds);}else{ selectUrl=ConstantURL.RECOVER_RESUME_CLIP; params.addBodyParameter("companyId", "535662D0416911E5A076F8F061532E9D");params.addBodyParameter("favoritesIds", node.getId());} requestUrl(HttpMethod.POST,selectUrl, params,new RequestCallBack<String>() { @Override public void onFailure(HttpException arg0, String err) {Toast.makeText(getApplicationContext(), "end错误!", 2000).show();} @Override public void onSuccess(ResponseInfo<String> info) {Gson gson=new Gson(); GsonDataBean bean=gson.fromJson(info.result, GsonDataBean.class);String msg=bean.message; Toast.makeText(getApplicationContext(),msg, 2000).show(); }}); Toast.makeText(getApplicationContext(), node.getText(),Toast.LENGTH_SHORT).show(); 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对象解析
- python中文处理
- 再用UICollectionView做无限轮播的时候,布局错乱出现黑边,很可能是
- mybatis的mapper
- MySql模糊查询like通配符使用详细介绍
- 数据结构-题目
- android中树形json解析为对象,并通过dialog显示,多级列表
- 正则表达式学习参考
- Windows 2008终端服务授权激活(解决远程桌面连接数限制)
- [error handle][java script]如何做到js绑定事件的同时不执行事件
- ubuntu下进行文件编码转换(如将文件转为UTF8格式)
- iOS有关屏幕的方法大全
- 获取Android设备的方向
- OpenGL API 之 glTexImage2D
- 简单的文件复制类