• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Contact Notes not pulling correctly

TeamTJ

Member
I am working on an app which needs to pull the notes for the contacts.

I have the following code:
Code:
private void ParseAndLoaadNote2()
{
    //Clear the list
    results.clear();
    results2.clear();
    this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));

    //read the contacts
    String whereName = ContactsContract.Data.MIMETYPE + " = ?";
    String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
    ContentResolver contentResolver = this.getContentResolver();
    Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    while (nameCur.moveToNext()) 
    {
        String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
        String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
        String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
        Long contactId = nameCur.getLong(nameCur.getColumnIndex(ContactsContract.Contacts._ID));
        String note = getNote(contactId);

        results2.add(given + " - " + family + "- " + display + "\n" + note);
    }
    nameCur.close();
    
    //Display the lists
    this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results2));


}// End ParseAndLoaadNote2

private String getNote(long contactId) 
{               
    String note = null; 
    String[] columns = new String[] { ContactsContract.CommonDataKinds.Note.NOTE }; 
    String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] whereParameters = new String[]{Long.toString(contactId), ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; 
    Cursor contacts = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null); 
    if (contacts.moveToFirst()) 
    { 
        note = contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
    } 
    contacts.close(); 
    return note; 
}//End getNote
When I run it, however, some of my contacts show NULL for the notes even if there are really notes there and some show the wrong contact's notes.

I'm stumped as to how to go about fixing.

Any suggestions?

Thanks!
 
Back
Top Bottom