2012-09-04 78 views
0

我嘗試從接觸list.Here電話號碼和聯絡電話號碼是我的代碼,以顯示它...獲得來自聯繫人列表的名稱和數量

public class ContactTutorialActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 

startActivityForResult(intent, 1);     
    } 
@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (data != null) { 
     Uri uri = data.getData(); 

     if (uri != null) { 
      Cursor c = null; 
      try { 
       c = getContentResolver().query(uri, new String[] 
         { 
     ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.TYPE },null, null, null); 

    if (c != null && c.moveToFirst()) { 

    String number = c.getString(0); 

    int type = c.getInt(1); 

    showSelectedNumber(type, number); 

     } 
     } finally { 
     if (c != null) { 
     c.close(); 
     } 
     } 
     } 
    } 
} 
public void showSelectedNumber(int type, String number) 
{ 

    Toast.makeText(this, type + " " + number, Toast.LENGTH_LONG).show();  
} 
} 

那麼我希望是,如果我選擇任何的光標移動到第一個並顯示選定的名稱和編號 但在這裏當我運行程序光標移動到第一個吐司消息work.no聯繫人名稱和編號。

回答

1

你可以得到聯繫人的Uri ContactsContract.Contacts.CONTENT_URI

這是類似的例子:

ContentResolver cr = getContentResolver(); 
     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
       null, null, null, null); 
     Log.e("phonenu",""+cur.getCount()); 
     if (cur.getCount() > 0) 
     { 


     while (cur.moveToNext()) 
     { 
      String id = cur.getString(
         cur.getColumnIndex(ContactsContract.Contacts._ID)); 
     //String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     //Log.e("contacts", name); 
     if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
      //Query phone here. Covered next 
      Cursor pCur = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null); 
        while (pCur.moveToNext()) { 
        // Do something with phones 
        name.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))); 
        number.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 

        } 
        pCur.close(); 
     }}} 

另一個線程Here

Study here to know more

相關問題