2010.12.29——— android 可伸缩的listview ExpandableList

直接上代码吧 比较简单 大家看一下就明白了


  package com.huitu.project;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.huitu.pojo.ZFSCLX;import com.huitu.service.ZFSCLXService;import com.huitu.util.JSONUtil;import android.app.AlertDialog;import android.app.ExpandableListActivity;import android.app.ProgressDialog;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.KeyEvent;import android.view.View;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.SimpleExpandableListAdapter;public class ZFSCMainActivity extends ExpandableListActivity {private ExpandableListAdapter mAdapter;private ProgressDialog pd;private Handler handler;private List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>();    private List<List<Map<String, Object>>> childData = new ArrayList<List<Map<String, Object>>>();        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        pd = new ProgressDialog(this);pd.setIndeterminate(true);pd.setMessage("加载数据...");pd.setCancelable(true);pd.show();        handler = new Handler(){         @Override             public void handleMessage(Message msg) {         int length = msg.getData().getInt("length");             switch (length){             case 1 :             pd.dismiss();         setListAdapter(mAdapter);         break;             case 2 :             pd.dismiss();            new AlertDialog.Builder(ZFSCMainActivity.this)    .setMessage("网络异常!")           .setCancelable(false)           .setPositiveButton("确定", new DialogInterface.OnClickListener() {               public void onClick(DialogInterface dialog, int id) {               //finish();               }           }).create().show();    break;                      }         }        };                new Thread(){        Message msg = new Message();        public void run(){        try{        String json = ZFSCLXService.queryZFSCLX(null);        System.out.println(json);        List<ZFSCLX> list_zfsclx= JSONUtil.parseJSON_ZFSCLX_list(json);                        for (ZFSCLX bean : list_zfsclx) {                    Map<String, Object> curGroupMap = new HashMap<String, Object>();                    groupData.add(curGroupMap);                    curGroupMap.put("name", bean.getName());                    curGroupMap.put("id",bean.getId());                    curGroupMap.put("pid",bean.getP_Id());                                        String json2 = ZFSCLXService.queryZFSCLX(bean.getId());            System.out.println(json2);            List<ZFSCLX> zfsclxitems= JSONUtil.parseJSON_ZFSCLX_list(json2);                    List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();                    for (ZFSCLX bean2 : zfsclxitems) {                        Map<String, Object> curChildMap = new HashMap<String, Object>();                        children.add(curChildMap);                        curChildMap.put("name", bean2.getName());                        curChildMap.put("id",bean2.getId());                        curChildMap.put("pid",bean2.getP_Id());                    }                    childData.add(children);                }             // Set up our adapter                mAdapter = new SimpleExpandableListAdapter(                ZFSCMainActivity.this,                        groupData,                        android.R.layout.simple_expandable_list_item_1,                        new String[] { "name" },                        new int[] { android.R.id.text1 },                        childData,                        android.R.layout.simple_expandable_list_item_1,                        new String[] { "name" },                        new int[] { android.R.id.text1 }                        );                msg.getData().putInt("length", 1);    handler.sendMessage(msg);        }catch(Exception e){        e.printStackTrace();        msg.getData().putInt("length", 2);    handler.sendMessage(msg);        }        }        }.start();                                    }        @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// 按下键盘上返回按钮if (keyCode == KeyEvent.KEYCODE_BACK) {new AlertDialog.Builder(this).setMessage("确定退出系统吗?").setNegativeButton("取消",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {}}).setPositiveButton("确定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {finish();}}).show();return true;} else {return super.onKeyDown(keyCode, event);}}@Overrideprotected void onDestroy() {super.onDestroy();// 或者下面这种方式//System.exit(0);//建议用这种android.os.Process.killProcess(android.os.Process.myPid());}@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {String ID = (String)groupData.get(groupPosition).get("id");Intent intent = new Intent(ZFSCMainActivity.this,ZFSCActivity.class);Bundle bundle = new Bundle();bundle.putString("ID", ID);bundle.putInt("g_index", groupPosition);bundle.putInt("c_index", childPosition);bundle.putString("CID", childData.get(groupPosition).get(childPosition).get("id").toString());intent.putExtras(bundle);startActivity(intent);return super.onChildClick(parent, v, groupPosition, childPosition, id);}}



更多相关文章

  1. android按钮点击——implements View.OnClickListener
  2. 通过xml布局文件实现按钮改变焦点设置背景图片
  3. android 按钮水波纹效果【背景色】
  4. android 单选按钮组的使用
  5. android:动态创建多个按钮 并给每个按键添加监听事件
  6. 简单的中间文字两边按钮
  7. Android ImageSwitcher 实现按钮的3d旋转效果
  8. 设置 Toolbar(ActionBar) 上的按钮颜色

随机推荐

  1. Android(安卓)ArcGis 地图图层切换
  2. android >WebView 实现 JS , JAVA 互调
  3. Android(安卓)模拟登陆 保存密码(信息)到手
  4. Android(安卓)安装apk的方法
  5. Android监测前后台切换
  6. Android(安卓)的第一个游戏
  7. Android入门之相对布局(RelativeLayout)
  8. Android上的一些小技巧
  9. android -- notification使用(转)
  10. Android(安卓)自定义Button形状