2010.12.29——— android 可伸缩的listview ExpandableList

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();
        
        
        
        
    }
    
    @Override
	public 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);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 或者下面这种方式
		//System.exit(0);
		//建议用这种
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	@Override
	public 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);
	}
	
	
	
}



你可能感兴趣的:(bean,android,json,OS,UP)