Get sorting order preference selected user in Contacts>settings in Android Device and How to reduce query time?

我的梦境 提交于 2019-12-19 02:01:31

问题


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.


回答1:


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:

int sort_order=Settings.system.getInt (getApplicationContext ().getContentResolver (),"android.contacts.SORT_ORDER");

On my Samsung S2 device, sort_order = 1 when contacts are listed by firstname and sort_order = 2 when contacts are listed by lastname.




回答2:


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

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Cursor mCursor = getContacts();
    startManagingCursor(mCursor);
    // now create a new list adapter bound to the cursor.
    // SimpleListAdapter is designed for binding to a Cursor.
    ListAdapter adapter = new SimpleCursorAdapter(this, // Context.
        android.R.layout.two_line_list_item, // Specify the row template
                            // to use (here, two
                            // columns bound to the
                            // two retrieved cursor
                            // rows).
        mCursor, // Pass in the cursor to bind to.
        // Array of cursor columns to bind to.
        new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME },
        // Parallel array of which template objects to bind to those
        // columns.
        new int[] { android.R.id.text1, android.R.id.text2 });

    // Bind to our new adapter.
    setListAdapter(adapter);
  }

  private Cursor getContacts() {
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
        + ("1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
        + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs,
        sortOrder);
  }

Here Sorting order is managed through the cursor , which contains the data for the list .



来源:https://stackoverflow.com/questions/19569896/get-sorting-order-preference-selected-user-in-contactssettings-in-android-devic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!