title: Get first and last name of a contact rather than single display name?
body: <p>I am currently working with the Android Contacts content provider and currently can access a contacts full display name without issue using the following code:</p>
  
  <pre><code>String[] PROJECTION = new String[] {
           ContactsContract.Contacts._ID,
           ContactsContract.Contacts.DISPLAY_NAME,
           ContactsContract.Contacts.HAS_PHONE_NUMBER,
          };
  String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
  
  String SELECTION = "LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ")" 
      + " LIKE '" + constraint + "%' " + "and " + 
          ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
  
  Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI, 
                  PROJECTION, SELECTION, null, sortOrder);
  </code></pre>
  
  <p>However I want to be able to get both the contacts first and last name separately, I have tried to use the StructuredName in an attempt to get this but I can't seem to get it working.</p>
  
  <p>Can anyone point me in the right direction as to how to use the StructuredName correctly to get the name split into First and Last?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Following hovanessyan's advice I have attempted the following:</p>
  
  <pre><code>    String[] PROJECTION = new String[] {
              ContactsContract.Data._ID,
              ContactsContract.Data.DISPLAY_NAME,
              ContactsContract.Data.HAS_PHONE_NUMBER,
      };
  
      String SELECTION = ContactsContract.CommonDataKinds.StructuredName.IN_VISIBLE_GROUP + " = '1'";
  
      String sortOrder = ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
  
      Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, sortOrder);
  
      int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
      int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
      int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
  
      while (cursor.moveToNext()) {
          String given = cursor.getString(indexGivenName);
          String family = cursor.getString(indexFamilyName);
          String display = cursor.getString(indexDisplayName);
          Log.e("XXX", "Name: | " + given + " | " + family + " | " + display);
      } 
  </code></pre>
  
  <p>However using the PROJECTION causes a crash as follows:</p>
  
  <pre><code>12-08 16:52:01.575: E/AndroidRuntime(7912): Caused by: java.lang.IllegalArgumentException: Invalid column has_phone_number
  </code></pre>
  
  <p>If I remove the PROJECTION I get all the results printed out but a lot of them contain NULL.</p>
  
  <p>For example:</p>
  
  <pre><code>12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
  12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
  12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
  12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
  </code></pre>
  
  <p>So can anyone see what I'm doing wrong in that my PROJECTION doesn't work?</p>
  
  <p><strong>FURTHER UPDATE:</strong></p>
  
  <p>I have sorted out the issues with my PROJECTION but now I have an issue where the DATA content provider is supplying me back with all the null data and causing NULL pointer exceptions in my code.</p>
  
  <p>A cursor count from ContactsContract.Contacts gives me back 115 for example but using the DATA table gives me back 464 using the same parameters and this is causing huge issues in my app.</p>
  
  <p>Anyone have any ideas why that is?</p>