android多选联系人实现
android多选联系人实现
可结合此方案更有效
- publicclassCopyContactsListMultipleextendsListActivityimplementsOnClickListener{
- privatefinalintUPDATE_LIST=1;
- ArrayList<String>contactsList;//得到的所有联系人
- ArrayList<String>getcontactsList;//选择得到联系人
- privateButtonokbtn;
- privateButtoncancelbtn;
- privateProgressDialogproDialog;
- Threadgetcontacts;
- HandlerupdateListHandler=newHandler(){
- publicvoidhandleMessage(Messagemsg){
- switch(msg.what){
- caseUPDATE_LIST:
- if(proDialog!=null){
- proDialog.dismiss();
- }
- updateList();
- }
- }
- };
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.contactslist);
- contactsList=newArrayList<String>();
- getcontactsList=newArrayList<String>();
- finalListViewlistView=getListView();
- listView.setItemsCanFocus(false);
- listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
- okbtn=(Button)findViewById(R.id.contacts_done_button);
- cancelbtn=(Button)findViewById(R.id.contact_back_button);
- okbtn.setOnClickListener(this);
- cancelbtn.setOnClickListener(this);
- getcontacts=newThread(newGetContacts());
- getcontacts.start();
- proDialog=ProgressDialog.show(CopyContactsListMultiple.this,"loading","loading",true,true);
- }
- @Override
- protectedvoidonResume(){
- //TODOAuto-generatedmethodstub
- super.onResume();
- }
- voidupdateList(){
- if(contactsList!=null)
- setListAdapter(newArrayAdapter<String>(this,
- android.R.layout.simple_list_item_multiple_choice,contactsList));
- }
- @Override
- protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
- //TODOAuto-generatedmethodstub
- if(!((CheckedTextView)v).isChecked()){
- CharSequencenum=((CheckedTextView)v).getText();
- getcontactsList.add(num.toString());
- }
- if(((CheckedTextView)v).isChecked()){
- CharSequencenum=((CheckedTextView)v).getText();
- if((num.toString()).indexOf("[")>0){
- StringphoneNum=num.toString().substring(0,(num.toString()).indexOf("\n"));
- getcontactsList.remove(phoneNum);
- Log.d("remove_num",""+phoneNum);
- }else{
- getcontactsList.remove(num.toString());
- Log.d("remove_num",""+num.toString());
- }
- }
- super.onListItemClick(l,v,position,id);
- }
- classGetContactsimplementsRunnable{
- @Override
- publicvoidrun(){
- //TODOAuto-generatedmethodstub
- Uriuri=ContactsContract.Contacts.CONTENT_URI;
- String[]projection=newString[]{
- ContactsContract.Contacts._ID,
- ContactsContract.Contacts.DISPLAY_NAME,
- ContactsContract.Contacts.PHOTO_ID
- };
- Stringselection=ContactsContract.Contacts.IN_VISIBLE_GROUP+"='1'";
- String[]selectionArgs=null;
- StringsortOrder=ContactsContract.Contacts.DISPLAY_NAME+"COLLATELOCALIZEDASC";
- Cursorcursor=managedQuery(uri,projection,selection,selectionArgs,sortOrder);
- Cursorphonecur=null;
- while(cursor.moveToNext()){
- //取得联系人名字
- intnameFieldColumnIndex=cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);
- Stringname=cursor.getString(nameFieldColumnIndex);
- //取得联系人ID
- StringcontactId=cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));
- phonecur=managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,null,null);
- //取得电话号码(可能存在多个号码)
- while(phonecur.moveToNext()){
- StringstrPhoneNumber=phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));
- if(strPhoneNumber.length()>4)
- contactsList.add("18610011001"+"\n测试");
- //contactsList.add(strPhoneNumber+"\n"+name+"");
- }
- }
- if(phonecur!=null)
- phonecur.close();
- cursor.close();
- Messagemsg1=newMessage();
- msg1.what=UPDATE_LIST;
- updateListHandler.sendMessage(msg1);
- }
- }
- @Override
- protectedvoidonPause(){
- //TODOAuto-generatedmethodstub
- super.onPause();
- }
- @Override
- protectedvoidonDestroy(){
- contactsList.clear();
- getcontactsList.clear();
- super.onDestroy();
- }
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- switch(v.getId()){
- caseR.id.contacts_done_button:
- Intenti=newIntent();
- if(getcontactsList!=null&&getcontactsList.size()>0){
- Bundleb=newBundle();
- b.putStringArrayList("GET_CONTACT",getcontactsList);
- i.putExtras(b);
- }
- setResult(RESULT_OK,i);
- CopyContactsListMultiple.this.finish();
- break;
- caseR.id.contact_back_button:
- CopyContactsListMultiple.this.finish();
- break;
- default:
- break;
- }
- }
- @Override
- publicbooleanonKeyDown(intkeyCode,KeyEventevent){
- //TODOAuto-generatedmethodstub
- if(keyCode==KeyEvent.KEYCODE_BACK){
- Intenti=newIntent();
- Bundleb=newBundle();
- b.putStringArrayList("GET_CONTACT",getcontactsList);
- i.putExtras(b);//}
- setResult(RESULT_OK,i);
- }
- returnsuper.onKeyDown(keyCode,event);
- }
- }
以上代码若有错误,请试用以下代码
class GetContacts implements Runnable {
@Override
public void run() {
Cursor cursor = null;
try {
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
cursor=managedQuery(uri, projection, selection, selectionArgs, sortOrder);
LogPrint.Print("lock", "cursor.getCount()======"+cursor.getCount());
if (cursor.getCount() > 0) {
while (cursor.moveToNext()){
// 取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);
String name = cursor.getString(nameFieldColumnIndex);
// 取得联系人ID
String contactId = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));
Cursor phonecur = managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
// 取得电话号码(可能存在多个号码)
LogPrint.Print("lock", "phonecur.getCount()======"+phonecur.getCount());
if (phonecur.getCount() > 0) {
while (phonecur.moveToNext()){
String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));
if(strPhoneNumber.length()>4)
person = new Person();
person.setName(name);
person.setPhone(strPhoneNumber);
contactsList.add(person);
}
if (phonecur != null){
phonecur.close();
}
}
}
Message msg1 = new Message();
msg1.what = UPDATE_LIST;
updateListHandler.sendMessage(msg1);
}else{
//未找到任何联系人
Message msg1 = new Message();
msg1.what = 1001;
updateListHandler.sendMessage(msg1);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if (cursor != null) {
cursor.close();
}
}
}
}
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ListViewandroid:id="@+id/android:list"
- android:layout_height="fill_parent"
- android:layout_width="fill_parent"
- android:layout_marginLeft="10dip"
- android:layout_marginRight="10dip"
- android:layout_marginTop="10dip"
- android:layout_weight="1.0">
- </ListView>
- <LinearLayoutandroid:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0"android:orientation="horizontal"
- android:gravity="center"android:layout_marginLeft="10dip"
- android:layout_marginRight="10dip"android:layout_marginBottom="10dip"
- android:weightSum="1">
- <Buttonandroid:id="@+id/contacts_done_button"
- android:textSize="17dip"
- android:layout_marginRight="10dip"android:layout_width="0dip"
- android:layout_height="wrap_content"android:layout_weight="0.35"
- android:text="sure"/>
- <Buttonandroid:id="@+id/contact_back_button"
- android:layout_marginLeft="10dip"android:textSize="17dip"
- android:layout_width="0dip"android:layout_height="wrap_content"
- android:layout_weight="0.35"android:text="back"/>
- </LinearLayout>
- </LinearLayout>
更多相关文章
- Android利用WebService查询手机号码归属地
- Android中textView自动识别电话号码,电子邮件,网址(自动加连接)
- android获取手机号码以及imsi信息
- Android 学习笔记 Contacts (三)Contacts 查找,增加,更新,删除联系人
- Android联系人数据库全解析(2)
- android 通过广播获取指定联系人短信内容
- 转:Android联系人数据库全解析
- Android基于PinnedSectionListView实现联系人通讯录