2012-06-01 64 views

回答

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")); 
        } 
    
相關問題