此博客只实现了查询功能:
import java.util.ArrayList;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 定义两个List来封装系统的联系人信息、指定联系人的电话号码、Email等详情
final ArrayList<String> names = new ArrayList<String>();
final ArrayList<ArrayList<String>> details = new ArrayList<ArrayList<String>>();
//使用ContentResolver查找联系人的数据
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,null );
//遍历查询结果,获取系统中所有联系人
while(cursor.moveToNext()){
//获取联系人ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//获取联系人的名字
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
names.add(name);
//使用ContentResolver查询联系人的电话号码
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+"="+contactId, null, null);
ArrayList<String> detail = new ArrayList<String>();
//遍历查询结果,获取该联系人的多个电话号码
while(cursor.moveToNext()){
//获取查询结果中电话号码列中的数据
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
detail.add("电话号码:"+phoneNumber);
}
phones.close();
//使用ContentResolver查询联系人的E_mail地址
Cursor emails = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+"="+contactId, null, null);
//遍历查询结果,获取该联系人的多个E_mail地址
while(emails.moveToNext()){
//获取查询结果中E_mail地址列中的数据
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA
));
detail.add("邮件地址:"+emailAddress);
}
emails.close();
details.add(detail);
}
cursor.close();
//加载result.xml界面布局代表的视图
View resultDialog = getLayoutInflater().inflate(R.layout.result, null);
//获取resultDialog中ID为list的ExpandableListView
ExpandableListView list = (ExpandableListView) resultDialog.findViewById(R.id.list);
//创建一个ExpandableListAdapter对象
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
//该方法决定每个组选项的外观
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return names.size();
}
//获取指定组位置处的组数据
@Override
public Object getGroup(int groupPosition) {
return names.get(groupPosition);
}
@Override
public int getChildrenCount(int groupPosition) {
return details.get(groupPosition).size();
}
private TextView getTextView(){
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
//该方法决定每个子选项的外观
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//获取指定组位置、指定子列表项处的子列表项数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return details.get(groupPosition).get(childPosition);
}
};
//为ExpandableListView设置Adapter
list.setAdapter(adapter);
//使用对话框来显示查询结果
new AlertDialog.Builder(MainActivity.this)
.setView(resultDialog).setPositiveButton("确定", null).show();
}
});