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;
}
I fixed this problem by passing an ID corresponding to the line I want to edit in the contacts database.
Related
I am getting only name & birthday using the code below. But I need phone number & email also. It would be great if anyone can help me out. Thanks.
private void getContacts() {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Event.START_DATE
};
String where = ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[]{
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
};
String sortOrder = null;
ContentResolver contentResolver = this.getActivity().getContentResolver();
Cursor cursor = contentResolver.query(uri, projection, where, selectionArgs, sortOrder);
int nameColumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int emailColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
int bithDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
while (cursor.moveToNext()) {
String name = cursor.getString(nameColumn);
String number = cursor.getString(numberColumn);
String email = cursor.getString(emailColumn);
String birthDay = cursor.getString(bithDayColumn);
Log.d(TAG, "Birthday: " + birthDay);
}
}
In your projection you're limiting your query to rows of MIMETYPE CommonDataKinds.Event.CONTENT_ITEM_TYPE only, so you'll only get birthdays.
You need to ask for emails and phones mimetypes, but note that these additional information will come in separate rows for the same contact.
For example, for contact A that has 2 phones, 3 emails and a birthday, you'll get 6 results in your cursor. So you need to group them all together using the CONTACT_ID field.
Here's simple code to get you started, print the resulting HashMap and you'll get for each contact all his/hers name, emails, phones and birthday:
Map<Long, List<String>> contacts = new HashMap<Long, List<String>>();
String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3};
// query only emails/phones/events
String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Event.CONTENT_ITEM_TYPE"', '" + Email.CONTENT_ITEM_TYPE + "')";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1); // full name
String mime = cur.getString(2); // type of data (phone / birthday / email)
String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234
String kind = "unknown";
switch (mime) {
case Phone.CONTENT_ITEM_TYPE:
kind = "phone";
break;
case Event.CONTENT_ITEM_TYPE:
kind = "birthday";
break;
case Email.CONTENT_ITEM_TYPE:
kind = "email";
break;
}
Log.d(TAG, "got " + id + ", " + name + ", " + kind + " - " + data);
// add info to existing list if this contact-id was already found, or create a new list in case it's new
List<String> infos;
if (contacts.containsKey(id)) {
infos = contacts.get(id);
} else {
infos = new ArrayList<String>();
infos.add("name = " + name);
contacts.put(id, infos);
}
infos.add(kind + " = " + data);
}
The Cons.getpersonIdForRegisteredUser() method returns a string.
My query has two parts: one is to check the person id and the other is data enabled
#Override
public Loader<Cursor> onCreateLoader(final int i, final Bundle bundle) {
String sortOrder = ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_FIRST_NAME
+ " COLLATE NOCASE ASC";
String selection = ContactProviderContract.
ContactDatabaseEntry.COLUMN_NAME_PERSON_ID + " != " + Cons.getPersonIdForRegisteredUser(getActivity().getApplicationContext())
+ " AND "
+ ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_DATA_ENABLED + " = ?";
String[] selectionArgs = { "1" };
return new CursorLoader(getActivity().getApplicationContext(),
ContactProviderContract.CONTACTS_URI,
ContactProviderContract.ContactDatabaseEntry.MIN_DETAILS_COLUMNS,
selection,
selectionArgs,
sortOrder);
}
The error is that the string retrieved from the method Cons.getpersonid...() is considered as a column and not as a string to compare.
If I understand the error correctly, then you should place that Cons.getPersonId... into the selectionArgs.
String selection = ContactProviderContract.
ContactDatabaseEntry.COLUMN_NAME_PERSON_ID + " != ?" +
+ " AND "
+ ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_DATA_ENABLED + " = ?";
String[] selectionArgs = { "1" , Cons.getPersonIdForRegisteredUser(getActivity().getApplicationContext()) };
I have two parameter (tenant_id and dateIn). with my query below I always get -1 as result. I don't know where is the issue in query statement.
Kindly note that the date format in data base is dd/MM/yyyy
public final static String ID = "id";
public final static String TENANT_ID = "tenant_id";
public final static String DEBT_CURRENT_VALUE = "debt_current_value";
public final static String DEBT_SUM = "debt_sum_value";
public final static String DEBT_DATE = "debt_date";
public long getTenantDateDebtByIdandDate(long id, String dateIn) {
long idout = -1;
String[] columns = {ID, TENANT_ID, DEBT_CURRENT_VALUE,DEBT_SUM, DEBT_DATE };
String selection = "TENANT_ID = " + id + " AND " + "DEBT_DATE < " + "date("+ dateIn+")";
Cursor cursor = mDatabase.query(TABLE_TENANT_DEBT, columns, selection, null, null, null, null);
if (cursor.moveToNext()) {
idout = cursor.getLong(cursor.getColumnIndex(DEBT_DATE));
}
cursor.close();
return idout;
}
I think there is a mistake with the column names at least
Isn't this: String selection = "TENANT_ID = " + id + " AND " + "DEBT_DATE < " + "date("+ dateIn+")"; checking columns named TENANT_ID and DEBT_DATE, when actually you want to check columns "tenant_id" and "debt_date".
So I think the selection should be instead: String selection = TENANT_ID + " = " + id + " AND " + DEBT_DATE + " < " + "date("+ dateIn+")";
I'm not so familiar with dates in sqlite, but if the date function is not working in this case, maybe you should look into the strftime function of sqlite
Also, I would recommend using the where arguments. So basically instead of saying TENANT_ID + " = " + id, you would say TENANT_ID + " = ?", and change the DEBT_DATE in similar way. Then you would give the query function one more array having the values that are then used in the places of the questionmarks in the order they are in the array. The query is safer that way, see more details why to use the where args and a complete example in this Stackoverflow thread in case you are interested.
Finally with the feedback of Peter and some resaerch I found the solution
public String getTenantDateDebtByIdandDate(long id, String dateIn) {
String idout = "";
String[] columns = {TENANT_ID, DEBT_DATE };
String selection = TENANT_ID + " = '" + id + "' AND " + DEBT_DATE + " < " + "'"+ dateIn + "'";
Cursor cursor = mDatabase.query(TABLE_TENANT_DEBT, columns, selection, null, null, null, null);
while (cursor.moveToNext()) {
idout = cursor.getString(cursor.getColumnIndex(DEBT_DATE));
}
cursor.close();
return idout;
}
I'm trying to go over all contacts using the contacts, raw_contacts and data tables.
From reading online I believe that Contacts._id is connected with RawContacts.contact_id and Data.raw_contact_id is connected with RowContacts._id. Am I right?
Believing in this I built this method:
public void testingContactsDatabase(String contactId)
{
final String[] projection = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME,
Data.RAW_CONTACT_ID,
Data.MIMETYPE,
StructuredName.DISPLAY_NAME,
StructuredName.FAMILY_NAME,
StructuredName.GIVEN_NAME,
StructuredName.MIDDLE_NAME,
StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
};
final String selection = Contacts._ID + " = " + contactId + " AND "
+ Contacts._ID + " = " + RawContacts.CONTACT_ID + " AND "
+ Data.RAW_CONTACT_ID + " = " + RawContacts._ID;
final Cursor curStructuredName = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
projection,
selection, null,
//new String[] {contactId, ContactsContract.RawContacts.CONTACT_ID, RawContacts._ID},
null
);
if(curStructuredName.moveToFirst())
{
Log.d("XYZ", "Found something");
}
}
But it never finds anything. I'm not sure if I'm using the correct URI, I've tried with different URIs and either got exceptions or same ouput.
Can someone point me out where I'm doing something wrong?
Thanks
I've solved my problem. Here is my code working now:
// Get the structured name fields
public ArrayList<String> getStructuredName(String contactId)
{
ArrayList<String> structuredList = new ArrayList<String>();
final String[] projection = new String[] {
StructuredName.DISPLAY_NAME,
StructuredName.FAMILY_NAME,
StructuredName.GIVEN_NAME,
StructuredName.MIDDLE_NAME,
StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
};
final String filter = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
final String parameters[] = {StructuredName.CONTENT_ITEM_TYPE, contactId};
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection, filter, parameters, null);
if(cursor.moveToFirst())
{
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
final String displayName = cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME));
final String familyName = cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
final String givenName = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));
final String middleName = cursor.getString(cursor.getColumnIndex(StructuredName.MIDDLE_NAME));
final String prefix = cursor.getString(cursor.getColumnIndex(StructuredName.PREFIX));
final String suffix = cursor.getString(cursor.getColumnIndex(StructuredName.SUFFIX));
structuredList.add(displayName);
structuredList.add(familyName);
structuredList.add(givenName);
structuredList.add(middleName);
structuredList.add(prefix);
structuredList.add(suffix);
Log.d(tag, "/////////////////////////////////////////////////////");
Log.d(tag, "displayName: " + displayName );
Log.d(tag, "familyName: " + familyName );
Log.d(tag, "givenName: " + givenName );
Log.d(tag, "middleName: " + middleName );
Log.d(tag, "prefix: " + prefix );
Log.d(tag, "suffix: " + suffix );
Log.d(tag, "/////////////////////////////////////////////////////");
}
}
cursor.close();
return structuredList;
}
I am developing an app in which i want to access MISSED_CALL log. Using below code....
private Cursor getItemsToSync() {
G = "Log method accessing";
ContentResolver r = getContentResolver();
String selections = String.format("%s > ?", CallLog.Calls.DATE,CallLog.Calls.MISSED_TYPE);
String[] selectionArgs = new String[] { String.valueOf(getMaxSyncedDate())};
String sortOrder = SmsConsts.DATE + " LIMIT " + PrefStore.getMaxItemsPerSync(this);
N = CallLog.Calls.CACHED_NAME;
return r.query(Uri.parse("content://call_log/calls"), null,selections,selectionArgs, sortOrder);}
its provide All Call Log. Please suggest me how to get only MISSED_CALL Call log. Thanks in advance
String[] strFields = {android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.NUMBER,android.provider.CallLog.Calls.DATE, android.provider.CallLog.Calls.TYPE
};
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor mCallCursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,strFields, null, null, strOrder);
if (mCallCursor.moveToFirst()) {
do {
boolean missed = mCallCursor.getInt(mCallCursor.getColumnIndex(CallLog.Calls.TYPE)) == CallLog.Calls.MISSED_TYPE;
if (missed) {
String name = mCallCursor.getString(mCallCursor
.getColumnIndex(CallLog.Calls.CACHED_NAME));
String number = mCallCursor.getString(mCallCursor
.getColumnIndex(CallLog.Calls.NUMBER));
Log.d("PhoneLog", "You have a missed call from " + name + " on " + number // + " at " + time); }
} while (mCallCursor.moveToNext());
}