-1

我想选择构建一个应用程序,该应用程序包括从本地联系人中选择电子邮件 ID。如何在应用程序中获取选定的电子邮件值。如何实现这一点,任何独立示例都会很有用。谢谢。

4

1 回答 1

1
  1. 实现联系人列表如下:

       public class contDetail extends Activity implements OnItemClickListener {    
    private static final String[] PROJECTION = new String[] {            
    ContactsContract.Contacts._ID,            
    ContactsContract.Contacts.DISPLAY_NAME,            
    ContactsContract.Contacts.HAS_PHONE_NUMBER,            
    ContactsContract.Contacts.LOOKUP_KEY  
    };
    private int mIdColumnIndex;
    private int mHasPhoneColumnIndex;
    private TextView mPhone;
    @Override    
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
           setContentView(R.layout.pickcontact);
       mPhone = (TextView) findViewById(R.id.test); 
    ListView myList=(ListView)findViewById(R.id.list1); 
    Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI,
            PROJECTION, null, null, null);       
            mIdColumnIndex = c.getColumnIndex(ContactsContract.Contacts._ID);      
            mHasPhoneColumnIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
            ListAdapter adapter = new SimpleCursorAdapter(this, 
                    android.R.layout.simple_list_item_1,c, 
                    new String[] { ContactsContract.Contacts.HAS_PHONE_NUMBER },
                    new int[] { android.R.id.text1 }); 
            myList.setAdapter(adapter); 
            myList.setOnItemClickListener(this);
            //setOnItemSelectedListener(this);      
    
       }
    
       public void onItemClick(AdapterView<?>parent, View v, int position, long id) {
    if (position >= 0) { 
    final Cursor c = (Cursor) parent.getItemAtPosition(position); 
    if (c.getInt(mHasPhoneColumnIndex) > 0) { 
    final long contactId = c.getLong(mIdColumnIndex);   
    final Cursor phones = getContentResolver().query(  
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
    new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, 
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, 
    ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY + " DESC"); 
    try {             
    phones.moveToFirst();   
        mPhone.setText(phones.getString(0));
        String ph = phones.getString(0);
        Intent intent = this.getIntent(); 
        intent.putExtra("SOMETHING",ph ); 
        this.setResult(RESULT_OK, intent); 
        finish(); 
    
    } finally {                 
    phones.close();    
    }          
    } else {  
    mPhone.setText(R.string.button_info_text);  
    }}  
       }
    
       public void onNothingSelected(AdapterView parent) {
    mPhone.setText(R.string.button_info_text);
    }
    
     }
    
  2. 将所选联系人复制到您的活动的 EditText 框中,如下所示:

          @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data)                  {
      super.onActivityResult(requestCode, resultCode, data);
      if(resultCode==RESULT_OK && requestCode==1)       
       ed1.setText(data.getStringExtra("SOMETHING"));
      if(resultCode==RESULT_OK && requestCode==2)       
         ed2.setText(data.getStringExtra("SOMETHING"));   
       }
    
于 2012-06-01T08:53:10.363 回答