0

我知道你可能会认为这个主题已经被处理了好几次,但这次是不同的!

我的应用程序应该从选定的联系人那里获取联系信息(姓名、号码),但我只得到姓名而无法得到号码。

@Override
public void onClick(View v) {
   // Opening Contacts Window as a Window
   Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, 
                                     ContactsContract.Contacts.CONTENT_URI);  
       // calling OnActivityResult with intent And Some contact for Identifie
   startActivityForResult(contactPickerIntent, PICK);  
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
  super.onActivityResult(reqCode, resultCode, data);
  switch (reqCode) {
    case (PICK) :
      if (resultCode == Activity.RESULT_OK) {
         Uri contactData = data.getData(); 
         Cursor c =  managedQuery(contactData, null, null, null, null);
         if (c.moveToFirst()) {

            int indexName = c.getColumnIndex(
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int indexNumber = c.getColumnIndex(
                        ContactsContract.CommonDataKinds.Phone.NUMBER);
            nom   = c.getString(indexName);
            numero = c.getString(indexNumber);


           //Visual confirm
           Toast.makeText(this, "Contact " + nom +" enregistré!",
                          Toast.LENGTH_LONG).show();

           //Save in prefs:
           SharedPreferences manager = 
                         PreferenceManager.getDefaultSharedPreferences(this);
           Editor editor = manager.edit();
           editor.putString("num", numero);             
           editor.putString("nom", nom);
           editor.commit();

名称是正确的,但数字会导致强制关闭。

但是如果我用下面的替换它不再强制关闭,但数字仍然不正确(0或1)。

int indexNumber = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)+1;

我应该怎么办?

4

1 回答 1

0
private void getDetails(){
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER };
        Cursor names = getContentResolver().query(uri, projection, null, null, null);

        int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        names.moveToFirst();
        do {

            String name   = names.getString(indexName);
            Log.e("Name new:", name);
            String number = names.getString(indexNumber);
            Log.e("Number new:","::"+number);

        } while (names.moveToNext());
    }

以上从您的联系人数据库中重新运行所有姓名和号码。

如果您甚至需要电子邮件 ID,您还可以添加这些行:::

while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor email = cr.query( 
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                            new String[]{id}, null); 
            while (email.moveToNext()) { 
                //to get the contact names
                // if the email addresses were stored in an array
                String emailid = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e("Email id ::", emailid);
                String emailType = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
                Log.e("Email Type ::", emailType);

            } 
            email.close();
        }
于 2012-04-24T03:14:16.457 回答