【问题标题】:How to select multiple contacts at a time?如何一次选择多个联系人?
【发布时间】:2011-04-15 10:57:10
【问题描述】:

我使用以下代码将电话簿联系人检索到我的应用程序:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
                                            Contacts.CONTENT_URI);  
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);      
}

但我想选择多个联系人并将其上传到数据库。这可能吗?如果可以,我该怎么做?

【问题讨论】:

    标签: android database android-contacts android-contentresolver


    【解决方案1】:

    您可以直接在您的活动中查询内容提供者,以便用户可以选择多个联系人。这可以使用联系人合约来实现。有关更多信息,您可以查看api documentation 或此tutorial blog post

    【讨论】:

      【解决方案2】:

      试试这个代码..

           people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
          int position=0;
          Cursor q=db.query(mProfile,new String[]{"person_name"},"person_name"+"!='"+null+"'",null,null, null, null);
          people.moveToFirst();
              int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);    
              while(!people.isAfterLast()) {
                  Cursor o=db.query(mProfile,new String[]{"person_name"},"person_name"+"='"+people.getString(nameFieldColumnIndex)+"'",null,null, null, null);
                  if(!(o.getCount()>0))
                  {  
                  mConname.add(position, people.getString(nameFieldColumnIndex));
                  try{
                      String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
                      String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                      if ( hasPhone.equalsIgnoreCase("1"))
                          hasPhone = "true";
                      else
                          hasPhone = "false" ;
                      if (Boolean.parseBoolean(hasPhone)) 
                      {
                          Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                          while (phones.moveToNext()) 
                          {
                              String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                              mConno.add(position,phoneNumber);
      
                          }
                          phones.close(); 
                      }   
                      if(hasPhone=="false")
                      {   mConname.remove(position);
                      }   
                      else
                          position++;
                  }       
                  catch(Exception e)
                  { 
      
                  }
              }
                  people.moveToNext();
              }
      

      使用 sqlite 并存储它

      【讨论】:

      • 什么是“mProfile”,您能否提供更多详细信息
      猜你喜欢
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2012-02-27
      相关资源
      最近更新 更多