1. 在splash加载时将常用号码数据库拷贝到系统目录下
2.创建数据库访问dao层
3.创建MyAdapter类extends BaseExpandableListAdapter实现未实现的方法
4.给elv中的孩子设置点击事件
--------------------------------------------------------------------
2.创建数据库访问dao层
public
class
CommonNumberDao {
private
static
final
String
path
=
"/data/data/com.itheima.mobilesafe/files/commonnum.db"
;
/**
* 返回分组里面有多少个条目
*
@return
*/
public
static
int
getGroupCount(){
SQLiteDatabase db = SQLiteDatabase. openDatabase(
path
,
null
, SQLiteDatabase.
OPEN_READONLY
);
Cursor cursor = db.rawQuery(
"select count(*) from classlist"
,
null
);
cursor.moveToFirst();
int
count = cursor.getInt(0);
cursor.close();
db.close();
return
count;
}
/**
* 返回每个分组里面有多少个孩子条目
*
@return
*/
public
static
int
getChildCountByPosition(
int
groupPosition){
SQLiteDatabase db = SQLiteDatabase. openDatabase(
path
,
null
, SQLiteDatabase.
OPEN_READONLY
);
int
newpositions = groupPosition+1;
Cursor cursor = db.rawQuery(
"select count(*) from table"
+newpositions,
null
);
cursor.moveToFirst();
int
count = cursor.getInt(0);
cursor.close();
db.close();
return
count;
}
//select name from classlist where idx=2
/**
* 返回某个位置分组的名称
*
@param
groupPositions
*
@return
*/
public
static
String getGroupName(
int
groupPosition){
String name =
null
;
SQLiteDatabase db = SQLiteDatabase. openDatabase(
path
,
null
, SQLiteDatabase.
OPEN_READONLY
);
int
newpositions = groupPosition + 1;
Cursor cursor = db.rawQuery(
"select name from classlist where idx=?"
,
new
String[]{newpositions+
""
});
if
(cursor.moveToFirst()){
name = cursor.getString(0);
}
cursor.close();
db.close();
return
name;
}
/**
* 获取所有的分组名称
*
@return
*/
public
static
List<String> getGroupNames(){
List<String> groupNames =
new
ArrayList<String>();
SQLiteDatabase db = SQLiteDatabase. openDatabase(
path
,
null
, SQLiteDatabase.
OPEN_READONLY
);
Cursor cursor = db.rawQuery(
"select name from classlist "
,
null
);
while
(cursor.moveToNext()){
String name = cursor.getString(0);
groupNames.add(name);
}
cursor.close();
db.close();
return
groupNames;
}
/**
* 获取某个分组里面的所有的孩子
*
@return
*/
public
static
List<String> getChildNamesByPositions(
int
groupPosition){
List<String> childNames =
new
ArrayList<String>();
SQLiteDatabase db = SQLiteDatabase. openDatabase(
path
,
null
, SQLiteDatabase.
OPEN_READONLY
);
int
newgroupPositions = groupPosition + 1;
Cursor cursor = db.rawQuery(
"select name,number from table"
+newgroupPositions,
null
);
while
(cursor.moveToNext()){
String name = cursor.getString(0);
String number = cursor.getString(1);
childNames.add(name+
"\n"
+number);
}
cursor.close();
db.close();
return
childNames;
}
/**
* 返回某个位置分组的某个子孩子的名称
*
@param
groupPositions
*
@return
*/
public
static
String getChildNameByPosition(
int
groupPosition,
int
childPosition){
String name =
null
;
String number =
null
;
SQLiteDatabase db = SQLiteDatabase. openDatabase(
path
,
null
, SQLiteDatabase.
OPEN_READONLY
);
int
newgroupPositions = groupPosition + 1;
int
newchildPositions = childPosition + 1;
Cursor cursor =db.rawQuery(
"select name,number from table"
+newgroupPositions+
" where _id = ?"
,
new
String[]{newchildPositions+
""
});
if
(cursor.moveToFirst()){
name = cursor.getString(0);
number =cursor.getString(1);
}
cursor.close();
db.close();
return
name+
"\n"
+number;
}
}
---------------------------------------------------------------
3.创建MyAdapter类extends BaseExpandableListAdapter实现未实现的方法
4.给 elv中的孩子设置点击事件
public
class
ComNumActivity
extends
Activity {
private
ExpandableListView
elv_nomNumber
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_comnumber
);
//找到关心的控件
elv_nomNumber
=(ExpandableListView) findViewById(R.id.
elv_nomNumber
);
elv_nomNumber
.setAdapter(
new
MyAdapter());
//填充布局
//给elv 中的孩子设置点击事件
elv_nomNumber
.setOnChildClickListener(
new
OnChildClickListener() {
@Override
public
boolean
onChildClick(ExpandableListView parent, View v,
int
groupPosition,
int
childPosition,
long
id) {
//到孩子被点击时,获取孩子中的号码
String info = CommonNumberDao.getChildNameByPosition(groupPosition, childPosition);
String[] numbers=info.split(
"\n"
);
String number=numbers[1];
//得到孩子中的号码
//创建拨号意图
Intent intent =
new
Intent();
intent.setAction(Intent.
ACTION_DIAL
);
intent.setData(Uri. parse(
"tel:"
+number));
startActivity(intent);
return
false
;
}
});
}
/**
* 创建填充器
*/
public
class
MyAdapter
extends
BaseExpandableListAdapter{
/**
* 得到view的个数
*/
@Override
public
int
getGroupCount() {
return
CommonNumberDao. getGroupCount();
//查询数据库得到分组的个数
}
/**
* view孩子的个数
*/
@Override
public
int
getChildrenCount(
int
groupPosition) {
return
CommonNumberDao.getChildCountByPosition(groupPosition);
//得到对应分组的孩子个数
}
@Override
public
Object getGroup(
int
groupPosition) {
return
null
;
}
@Override
public
Object getChild(
int
groupPosition,
int
childPosition) {
return
null
;
}
@Override
public
long
getGroupId(
int
groupPosition) {
return
0;
}
@Override
public
long
getChildId(
int
groupPosition,
int
childPosition) {
return
0;
}
@Override
public
boolean
hasStableIds() {
return
false
;
}
/**
* 得到view显示的内容
*/
@Override
public
View getGroupView(
int
groupPosition,
boolean
isExpanded,
View convertView, ViewGroup parent) {
String name = CommonNumberDao.getGroupName(groupPosition);
//查询数据库得到对应位置view的内容
TextView tv=
new
TextView(getApplicationContext());
tv.setText(
" "
+name);
tv.setTextColor(Color.
BLACK
);
tv.setTextSize(22);
return
tv;
}
/**
* 得到view中孩子显示的内容
*/
@Override
public
View getChildView(
int
groupPosition,
int
childPosition,
boolean
isLastChild, View convertView, ViewGroup parent) {
//查询数据库得到对应小组的孩子的内容
String childName = CommonNumberDao.getChildNameByPosition(groupPosition, childPosition);
TextView tv=
new
TextView(getApplicationContext());
tv.setText(
" "
+childName);
tv.setTextColor(Color.
BLACK
);
tv.setTextSize(15);
return
tv;
}
/**
* view中的孩子是否可以相应点击事件
*/
@Override
public
boolean
isChildSelectable(
int
groupPosition,
int
childPosition) {
return
true
;
}
}
}