I have two questions:
1: How can my application know Default sorting order used for sorting native "Android Device Contacts"?
Inside "Contacts" -> "Settings" in android, we have "List by" and "Display Contacts by" options. How can I get these prefrences in my application.
For example: suppose device contacts are sorted by "First Name", then application should get some constant(or something similar). On the other hand, if contacts are sorted by "Last Name" then application should get constants(or info) regarding the same.
I searched SO and I got this link, but the problem is that the solution was deprecated in API level 5.
Here is my query for fetching contacts
String userPreference = getPrefContacts();/* **getPrefContacts() will return either ContactsContract.Data.DATA2 or ContactsContract.Data.DATA3** */
try {
ContentResolver cr = getActivity().getContentResolver();
String[] projectionName = new String[] {
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DATA2, ContactsContract.Data.DATA5,
ContactsContract.Data.DATA3, ContactsContract.Data.DATA1 };
String sortOrder = userPrefrence
+ " COLLATE LOCALIZED ASC";
Cursor nameCursor = cr.query(ContactsContract.Data.CONTENT_URI,
projectionName, null, null, sortOrder);
nameCursor.moveToFirst();
if (nameCursor.getCount() > 0) {
do {
String fName = nameCursor
.getString(nameCursor
.getColumnIndexOrThrow(ContactsContract.Data.DATA2));
String mName = nameCursor
.getString(nameCursor
.getColumnIndexOrThrow(ContactsContract.Data.DATA5));
String lName = nameCursor
.getString(nameCursor
.getColumnIndexOrThrow(ContactsContract.Data.DATA3));
String name = nameCursor
.getString(nameCursor
.getColumnIndexOrThrow(ContactsContract.Data.DATA1));
if (name != null) {
String[] projectionCommon = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.HAS_PHONE_NUMBER };
String selectionCommon = ContactsContract.Contacts.DISPLAY_NAME
+ " = ?";
String[] selectionArgCommon = new String[] { name };
Cursor common = cr.query(
ContactsContract.Contacts.CONTENT_URI,
projectionCommon, selectionCommon,
selectionArgCommon, null);
if (common.getCount() > 0) {
while (common.moveToNext()) {
String contactID = common
.getString(common
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
int hasPhone = common
.getInt(common
.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone > 0) {
String[] projectionPhone = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
String selectionPhone = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?";
String[] selectionArgPhone = new String[] { contactID };
Cursor phoneCursor = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projectionPhone,
selectionPhone,
selectionArgPhone, null);
if (phoneCursor.getCount() > 0) {
while (phoneCursor.moveToNext()) {
String phone = phoneCursor
.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phone != null) {
ContactData tmp = new ContactData(
name, fName, mName,
lName, phone, contactID);
if (raw.size() == 0) {
raw.add(tmp);
} else if (!(raw
.get(raw.size() - 1))
.getContactID()
.equalsIgnoreCase(
tmp.getContactID())) {
raw.add(tmp);
}
}
}
}
phoneCursor.close();
}
}
}
common.close();
}
} while (nameCursor.moveToNext());
nameCursor.close();
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
I need code for getPrefContacts() method
2: How to reduce the query time consumed with respect to above code or any other way to do so??
Hope anybody can help me.
Thanks in advance.
I can answer your second ques only.
You can use the join attribute of SQL when querying the content providers. Thereby, removing nested while loops and hence, reducing the query time.
Edit: For your first ques, try this:
On my Samsung S2 device, sort_order = 1 when contacts are listed by firstname and sort_order = 2 when contacts are listed by lastname.
you can manage the sorting order in your List Activity when you are creating your own CustomAdapter for the list activity .
Filtering and sorting of the data is handled by the adapter. You need to implement the logic in your custom adapter implementation.
The adapter is assigned to the ListView via the setAdapter method on the ListView object.
for eg
Here Sorting order is managed through the cursor , which contains the data for the list .