0
我正在創建編輯聯繫人字段的方法。 在編輯之前,我要確保這些字段確實存在,並且與我想要編輯的公司相對應,因此我做了一個選擇並尋找if(cursor.moveToFirst())
,但由於某些原因,我無法理解它不在if
之內。我只是知道問題出在我的空Strings
上,就好像我刪除了它們,代碼工作正常。查詢ContactsContract數據庫,將空字段與空字符串進行比較 - Android
我已經下載了手機聯繫人數據庫,所以我確切地知道聯繫人數據庫中的內容。這就是爲什麼我對變量進行硬編碼。
這裏是我的代碼:
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;
}
正如你所看到的,這些都是瓦爾很難與我我只是從手機下載的聯繫人數據庫上看到相應的值編碼。
oldCompany = "Tone Inc.";
oldType = "2";
oldLabel = "";
oldTitle = "The Big Boss";
oldDepartment = "";
oldJobDescription = "";
oldSymbol = "";
oldPhoneticName = "";
oldLocation = "";
但我始終得到logcat
的ERROR: Organization not found...
輸出任何人都能指出我哪裏是錯誤?
這裏是一個工作示例,如果我解開註釋代碼,我會得到未找到的輸出(與上面相同的問題)。與上面的模式相同,評論欄內沒有值。即使當我發送和空字符串爲什麼不匹配空與空?
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;
}