0

Can someone pls clarify how I can the phone number instead of checking if there is a phone number by ContactsContract.Contacts.HAS_PHONE_NUMBER. I tried ContractsContract.CommonKinds.Phone.NUMBER replacing has phone number but it continues to crash. The code below works with has phone number.

public class ContactListActivity extends ListActivity{ private Cursor cursor;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    /*

   ContactListCursorAdapter adapter = new ContactListCursorAdapter(getApplicationContext(), R.layout.contact_list_view,  cursor, from, to);
   setListAdapter(adapter);
   */
    setContentView(R.layout.settings);

    Cursor cur = getContacts();

    ListView lv = (ListView)findViewById(android.R.id.list);
    //String[] from = new String[] {ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.HAS_PHONE_NUMBER,ContactsContract.Contacts._ID};
   // int[] to = new int[] {R.id.checkBox};

   String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};

   SimpleCursorAdapter adapter = 
            new SimpleCursorAdapter(this, 
                                    R.layout.contact_list_item,
                                    cur,
                                    fields,
                                    new int[] {R.id.cbContact, R.id.tvPhoneNo});
      lv.setAdapter(adapter);      

}

 private Cursor getContacts() {  
        // Run query     
        Uri uri = ContactsContract.Contacts.CONTENT_URI;

        String[] projection = 
                new String[]{ ContactsContract.Contacts._ID,
                              ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER}; 
            String selection = null;
            String[] selectionArgs = null;  
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME +
                " COLLATE LOCALIZED ASC";  
            return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

}

user37375
  • 173
  • 1
  • 5
  • 22

2 Answers2

0

I have retrieved phone number from my callLog using this code .

Cursor cursor = contentResolver.query(uri, null, selection, selectionArgs,"date DESC");

  if (cursor != null && cursor.getCount() > 0) 
                { 
                 while (cursor.moveToNext()) 
                  { 

   phNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));

                      callDuration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));
                      dur = Integer.parseInt(callDuration);

 } 
               }  

              cursor.close();

hope this will help you.

Barun
  • 312
  • 3
  • 16
0

Resolved by using ContactsContract.CommonDataKinds.Phone.CONTENT_URI instead of ContactsContract as in the code below:

public class ContactListActivity extends ListActivity{ private Cursor cursor;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    /*

   ContactListCursorAdapter adapter = new ContactListCursorAdapter(getApplicationContext(), R.layout.contact_list_view,  cursor, from, to);
   setListAdapter(adapter);
   */
    setContentView(R.layout.settings);

    Cursor cur = getContacts();

    ListView lv = (ListView)findViewById(android.R.id.list);
    //String[] from = new String[] {ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.HAS_PHONE_NUMBER,ContactsContract.Contacts._ID};
   // int[] to = new int[] {R.id.checkBox};

   String[] fields = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};

   SimpleCursorAdapter adapter = 
            new SimpleCursorAdapter(this, 
                                    R.layout.contact_list_item,
                                    cur,
                                    fields,
                                    new int[] {R.id.cbContact, R.id.tvPhoneNo});
      lv.setAdapter(adapter);      

}

 private Cursor getContacts() {  
        // Run query     
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

        String[] projection = 
                new String[]{ ContactsContract.CommonDataKinds.Phone._ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; 
            String selection = null;
            String[] selectionArgs = null;  
            String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +
                " COLLATE LOCALIZED ASC";  
            return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

}

user37375
  • 173
  • 1
  • 5
  • 22