Java Question Name and Lastname of a contact

D

Deleted member 103

Guest
Hi,

I try with this code, the name and last name of a contact to get, unfortunately it does not work. Can someone tell me why?
B4X:
public String[] getFirstNameLastName(String id)
  {
      String[] retValue = new String[3];  
      retValue[0] = "";  
      retValue[1] = "";
      retValue[2] = ""; 

      String[] projection = new String[] {ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, ContactsContract.CommonDataKinds.StructuredName.SUFFIX, ContactsContract.CommonDataKinds.StructuredName.PREFIX, ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME};
     String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
     String[] whereParameters = new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};

     ContentResolver cr = this.m_ba.context.getContentResolver();
     Cursor contacts = cr.query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);

     if (contacts.moveToFirst())
     {
         Log.d("Log_Tag","ID=" + id);
        retValue[2] = contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
        retValue[0] = contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
        //String suffix_name = contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));
        //String prefix_name = contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
        //String middle_name = contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
        retValue[1] = contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
     }
     contacts.close();
     return retValue;
  }

Ciao,
Filippo
 

novaseer

Member
Licensed User
Longtime User
your code is kinda hard to read, but I think you're going wrong here:

B4X:
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
perhaps try
B4X:
String where = "ContactsContract.Data.CONTACT_ID = ? AND ContactsContract.Data.MIMETYPE = ?";
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…