2012-08-06 40 views
0

我需要將光標移動到,我有它的ID讀取其ID在Android數據庫

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
          null,null, null, null); 

特定聯繫人特定聯繫人我對所有的數據庫代碼,如何優化呢? 我需要將光標返回的姓名和聯繫

的默認數量謝謝

回答

0
cursor = getContentResolver().query(Phone.CONTENT_URI, 
          null, Phone.CONTACT_ID + "=?", new String[] { id }, 
          Phone.DISPLAY_NAME + " ASC"); 
0

你真正需要兩個光標。一個用於名稱& ID和另一個用於電話號碼。

Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

if (c.moveToFirst()) { 
    name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    fullName = c.getString(c.getColumnIndex("display_name_alt")); 
    id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); 

    if (Integer.parseInt(c.getString(c.getColumnIndex 
        (ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
     //phone number 

     Cursor phones = getContentResolver().query(Phone.CONTENT_URI, 
           null, 
           Phone.CONTACT_ID + " = " + id, 
           null, 
           null); 

     while (phones.moveToNext()) { 
       String number_type = phones.getString(phones.getColumnIndex 
           (ContactsContract.CommonDataKinds.Phone.TYPE)); 
       if(number_type.equalsIgnoreCase("1")) 
        number1 = phones.getString(phones.getColumnIndex 
           (ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       if(number_type.equalsIgnoreCase("2")) 
        number2 = phones.getString(phones.getColumnIndex 
           (ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     } 

     phones.close(); 
    } 
} 
c.close();