package com.milan.inzaghi09.activity;

import java.util.List;

import com.milan.inzaghi09.R;
import com.milan.inzaghi09.engine.CommonNumberDao;
import com.milan.inzaghi09.engine.CommonNumberDao.Child;
import com.milan.inzaghi09.engine.CommonNumberDao.Group;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

public class CommonNumberQueryActivity extends Activity {
	private ExpandableListView elv_common_number;
	private List<Group> mGroupList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_common_number_query);
		initUI();
		initData();
	}

	/**
	 * 给ExpandableListView控件准备展示用的数据
	 */
	private void initData() {
		CommonNumberDao dao = new CommonNumberDao();
		mGroupList = dao.getGroup(this);
		
		MyAdapter mAdapter = new MyAdapter();
		elv_common_number.setAdapter(mAdapter);
		//设置子条目点击事件
		elv_common_number.setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				startCall(mGroupList.get(groupPosition).childList.get(childPosition).number);
				
				return false;
			}
		});
	}
	
	/**
	 * 调用系统应用拨打电话
	 */
	protected void startCall(String number) {
		Intent intent = new Intent(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:"+number));
		startActivity(intent);
	}

	class MyAdapter extends BaseExpandableListAdapter{

		@Override
		public int getGroupCount() {
			return mGroupList.size();
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return mGroupList.get(groupPosition).childList.size();
		}

		@Override
		public Group getGroup(int groupPosition) {
			return mGroupList.get(groupPosition);
		}

		@Override
		public Child getChild(int groupPosition, int childPosition) {
			return mGroupList.get(groupPosition).childList.get(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			
			if (convertView == null) {
				convertView = View.inflate(getApplicationContext(), R.layout.elv_group_item, null);
			} 
			((TextView)convertView.findViewById(R.id.tv_name)).setText(getGroup(groupPosition).name);
			return convertView;
			
			
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(getApplicationContext(), R.layout.elv_child_item, null);
			} 
			((TextView)convertView.findViewById(R.id.tv_name)).setText(getChild(groupPosition, childPosition).name);
			((TextView)convertView.findViewById(R.id.tv_number)).setText(getChild(groupPosition, childPosition).number);
			return convertView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}}

	/**
	 * 初始化控件
	 */
	private void initUI() {
		elv_common_number = (ExpandableListView) findViewById(R.id.elv_common_number);
	}
}

@@ -1042,16 +1042,33 @@
 ta() %7B%0D%0A
+%09%09// 1%E5%88%9B%E5%BB%BA%E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D%E4%BD%9C%E5%AF%B9%E8%B1%A1%0D%0A
 %09%09Common
@@ -1107,16 +1107,35 @@
 Dao();%0D%0A
+%09%09// 2%E6%9F%A5%E8%AF%A2Group%E6%95%B0%E6%8D%AE%E9%9B%86%E5%90%88%0D%0A
 %09%09mGroup
@@ -1164,16 +1164,27 @@
 is);%0D%0A%09%09
+// 3%E6%9E%84%E5%BB%BA%E6%95%B0%E6%8D%AE%E9%80%82%E9%85%8D%E5%99%A8
 %0D%0A%09%09MyAd
@@ -1218,16 +1218,31 @@
 ter();%0D%0A
+%09%09// 4%E8%AE%BE%E7%BD%AE%E6%95%B0%E6%8D%AE%E9%80%82%E9%85%8D%E5%99%A8%0D%0A
 %09%09elv_co
@@ -1276,20 +1276,22 @@
 pter);%0D%0A
-
 %09%09//
+ 5
 %E8%AE%BE%E7%BD%AE%E5%AD%90%E6%9D%A1%E7%9B%AE%E7%82%B9%E5%87%BB%E4%BA%8B

add annotation