android显示列表的对象,android中树形json解析为对象,并通过dialog显示,多级列表

0818b9ca8b590ca3270a3433284dd417.png{"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;

}

}

// 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 lists = gson.fromJson(json.getString("children"),new TypeToken>() {}.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 arr = new ArrayList();ArrayList arr1;private void getDataFromListBean(List lists) {for (JsonBean jsonBean : lists) {           //将遍历到的jsonBean保存到list中arr.add(jsonBean);if (!"default".equals(jsonBean.type)) {arr1 = new ArrayList();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.youkuaiyun.com/lmj623565791/article/details/40212367

借用大牛的无限树,具体dialog用法就请大家参考大牛写的

public class ExpandDialog extends BaseActivity{//private List mDatas2 = new ArrayList();private List mDatas2 = new ArrayList();private ListView mTree;private TreeListViewAdapter mAdapter;String pageClick="0";String selectUrl="";ArrayList mylist;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.expand_dialog); mTree = (ListView) findViewById(R.id.id_tree);

//获取传过来的list集合mylist=(ArrayList)getIntent().getSerializableExtra("list");//System.out.println(mylist.size()); try{mAdapter = new SimpleTreeAdapter(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() {@Override public void onFailure(HttpException arg0, String err) {Toast.makeText(getApplicationContext(), "end错误!", 2000).show();} @Overridepublic void onSuccess(ResponseInfo 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);}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值