学习ContentProvider,例子——电话本
功能:
1、搜索已有联系人
2、添加新的联系人
搜索界面如下:
添加联系人界面如下:
Coding:
一、应用授权
1、授予读联系人
<uses-permission android:name="android.permission.READ_CONTACTS"/>
2、授予写联系人
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
二、搜索联系人
1、联系人有两张表,第一张表储存联系人的名字和ID号(独有的),第二张表储存联系人的详细信息如电话号码,E-mail等
final List<String> names = new ArrayList<String>(); final List<ArrayList<String>> details = new ArrayList<ArrayList<String>>();
其中,names储存第一张表的名字,details外部的list与names一一对应,内层的ArrayList保存了Phone、E-mail等信息。
2、获得第一张表的光标
Cursor cursor=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null ,null,null,null);
3、遍历第一张表,获取联系人的ID和名字
while (cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); if(name.contains(searchname)){ } else continue; String contactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); names.add(name);
4、根据当前的ID号,获得该联系人相关的第二张表中的Phone的光标,并建立存放该联系人详细信息的list——detail
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI ,null ,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactID ,null,null); ArrayList<String> detail = new ArrayList<String>();
5、将该联系人的所有的Phone存入detail中,并关闭Phone光标
while (phones.moveToNext()){ String phonenumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); detail.add("电话号码:"+phonenumber); } phones.close();
6、同样的方法获取该联系人的E-mail信息,存入detail,并关闭邮箱光标
while(email.moveToNext()){ String emailaddr = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); detail.add("邮箱地址:"+emailaddr); } email.close();
7、将该联系人的信息添加到用于储存表二信息的details中
details.add(detail);
8、关闭联系人光标
cursor.close();
9、使用BaseExpandableListAdapter来储存联系人及其详细数据
ExpandableListAdapter expandableListAdapter = new BaseExpandableListAdapter() { @Override public int getGroupCount() { return names.size(); } @Override public int getChildrenCount(int groupPosition) { return details.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return names.get(groupPosition); //? } @Override public Object getChild(int groupPosition, int childPosition) { return details.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } 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(30); return textView; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); 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 boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean hasStableIds() { return true; } };
10、使用ExpandableListView来显示联系人数据
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.showlist);
expandableListView.setAdapter(expandableListAdapter);
11、将以上10步作为函数放入EditText的Change监控函数中,一旦用户输入即EditText中的内容发生变化,则进行一次搜索
editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { EditText editText = (EditText) findViewById(R.id.searchtext); String searchString = editText.getText().toString(); listUI(searchString); }
三、添加联系人
1、设计添加联系人对话框,对话框布局在add_contact.xml中,onClick中将写入对话框确定时的事件。
final View dialog1 = getLayoutInflater().inflate(R.layout.add_contact,null); AlertDialog.OnClickListener listener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }; EditText editTextname = (EditText) dialog1.findViewById(R.id.nametext); editTextname.setText(searchString); new AlertDialog.Builder(MainActivity.this).setView(dialog1).setNegativeButton("取消", null).setPositiveButton("确定", listener).show();
2、从对话框的EditText中取出字符串
String name = ((EditText)dialog1.findViewById(R.id.nametext)).getText().toString(); String phone=((EditText)dialog1.findViewById(R.id.phonetext)).getText().toString(); String email=((EditText)dialog1.findViewById(R.id.emailtext)).getText().toString();
由于该EditText处于对话框中,需使用dialog1.findViewById
3、建立ContentValue,并向ContactsContract.RawContacts.CONTENT_URI中插入空值,以获得添加联系人表的新联系人ID
ContentValues contentValues = new ContentValues(); Uri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI,contentValues); long rawContactId = ContentUris.parseId(rawContactUri);
4、清空ContentValue,向其中添加联系人的ID,内容类型,联系人的内容等信息,并将该ContentValue加入表中
//添加联系人名字 contentValues.clear(); contentValues.put(ContactsContract.Data.RAW_CONTACT_ID,rawContactId); contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); contentValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,name); getContentResolver().insert(ContactsContract.Data.CONTENT_URI,contentValues); //添加联系人电话 contentValues.clear(); contentValues.put(ContactsContract.Data.RAW_CONTACT_ID,rawContactId); contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER,phone); contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); getContentResolver().insert(ContactsContract.Data.CONTENT_URI,contentValues); //添加联系人E-mail contentValues.clear(); contentValues.put(ContactsContract.Data.RAW_CONTACT_ID,rawContactId); contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE); contentValues.put(ContactsContract.CommonDataKinds.Email.DATA,email); contentValues.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK); getContentResolver().insert(ContactsContract.Data.CONTENT_URI,contentValues);
5、更新联系人列表
EditText editText = (EditText) findViewById(R.id.searchtext); String searchString = editText.getText().toString(); listUI(searchString);
6、将2~5步内容放入OnClick事件中。