Question

I'm creating methods to edit a contact's field. Before editing I'm making sure that those fields do exist and correspond to the company I want to edit, so I make a select and look for if(cursor.moveToFirst()) but it is not going inside the if for some reason I can not understand. I just know that the problem is on my empty Strings as if I remove them the code works fine.

I've downloaded the phones contact database so I know exactly what is in the contacts database. That is why I'm hardcoding the variables.

Here is my code:

public Boolean editRawContactOrganization(String rawContactId, String oldCompany, String oldType, String oldLabel, String oldTitle, 
        String oldDepartment, String oldJobDescription, String oldSymbol, String oldPhoneticName, String oldLocation,
        String newCompany, String newType, String newLabel, String newTitle, String newDepartment, String newJobDescription, 
        String newSymbol, String newPhoneticName, String newOfficeLocation)
{
    final ContentValues cv = new ContentValues();

    final String filter = Organization.RAW_CONTACT_ID + "=? AND " 
            + Organization.COMPANY + "=? AND "
            + Organization.TYPE_WORK + "=? AND "
            + Organization.LABEL + "=? AND "
            + Organization.TITLE + "=? AND "
            + Organization.DEPARTMENT + "=? AND "
            + Organization.JOB_DESCRIPTION + "=? AND "
            + Organization.SYMBOL + "=? AND "
            + Organization.PHONETIC_NAME + "=? AND "
            + Organization.OFFICE_LOCATION + "=? AND "
            + Data.MIMETYPE + "=?";

    oldCompany = "Tone Inc.";
    oldType = "2";
    oldLabel = "";
    oldTitle = "The Big Boss";
    oldDepartment = "";
    oldJobDescription = "";
    oldSymbol = "";
    oldPhoneticName = "";
    oldLocation = "";


    final String[] selectionArgs = new String[] {rawContactId, oldCompany, oldType, oldLabel, oldTitle, oldDepartment, oldJobDescription,
            oldSymbol, oldPhoneticName, oldLocation, Organization.CONTENT_ITEM_TYPE};

    final String[] projection = new String[] { Organization._ID };

    final Cursor cursor = contentResolver.query(Data.CONTENT_URI, 
            projection, 
            filter, 
            selectionArgs, 
            null);

    if(cursor.moveToFirst())
    {
        Log.d(TAG, "SUCCESS: Organization found! ID: " + cursor.getString(0));
        cursor.close();
    }
    else
    {
        Log.d(TAG, "ERROR: Organization not found...");
        cursor.close();
        return false;
    }

    return false;
}

As you can see, these vars are hard coded with the corresponding values I see on the contacts database I just downloaded from the phone.

oldCompany = "Tone Inc.";
oldType = "2";
oldLabel = "";
oldTitle = "The Big Boss";
oldDepartment = "";
oldJobDescription = "";
oldSymbol = "";
oldPhoneticName = "";
oldLocation = "";

But I always get the ERROR: Organization not found... output on logcat Can anyone point me to where is the error?

Here is a working example, if I uncoment the commented code I get the not found output (the same problem as above). Same pattern as above, the commented column has no value inside. Even when I'm sending and empty string why doesn't is match empty with empty?

public Boolean editRawContactWebsite(String rawContactId, String oldWebsite, String oldType, String oldLabel, 
        String newWebsite, String newType, String newLabel)
{
    final ContentValues cv = new ContentValues();

    final String filter = Website.RAW_CONTACT_ID + "=? AND " 
            + Website.URL + "=? AND "
            + Website.TYPE + "=? AND "
            //+ Website.LABEL + "=? AND "
            + Data.MIMETYPE + "=?";

    oldWebsite = "www.sitr.com";
    oldType = "7";
    oldLabel = "";


    final String[] selectionArgs = new String[] {rawContactId, oldWebsite, oldType, /*oldLabel,*/ Website.CONTENT_ITEM_TYPE};

    final String[] projection = new String[] { Website._ID };

    final Cursor cursor = contentResolver.query(Data.CONTENT_URI, 
            projection, 
            filter, 
            selectionArgs, 
            null);

    if(cursor.moveToFirst())
    {
        Log.d(TAG, "SUCCESS: website found! ID: " + cursor.getString(0));
        cursor.close();
    }
    else
    {
        Log.d(TAG, "ERROR: website not found...");
        cursor.close();
        return false;
    }
    return false;
}
Was it helpful?

Solution

I fixed this problem by passing an ID corresponding to the line I want to edit in the contacts database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top