如何显示电话簿中的联系人总数到文本视图

时间:2014-01-30 14:54:30

标签: android sqlite textview android-contacts

我能够获得联系人总数,但问题是,它来自sqlite,电话簿中的联系人数量与我从数据库中获得的数量不同。 这是我的代码:

Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        // Or this cursor
        Cursor cursor = managedQuery(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);

        int count = cursor.getCount();

        try {
            tv1.setText(String.valueOf(count));
            Log.i("Contacts: ", String.valueOf(count));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

我能够获得数据库中的联系人总数,但它与电话簿中显示的联系人不同。

任何建议或帮助都将非常感激。

1 个答案:

答案 0 :(得分:1)

我还在学习,所以我确信这比这更快,但我所做的只计算唯一名称是使用if语句来获取分配给我的唯一名称联系人列表并计算唯一名称的数量,这给了我一个准确的数字。这是我使用的代码,我希望有人可以接受并建议可能更有效的方法。希望这会有所帮助。

" nameCount"是你在这个例子中寻找的数字。

private void getContacts() {

        String name = "";
        String contact_id;
        int nameCount = 0;

        uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID };
        selection = null;
        selectionArgs = null;
        sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;

        Cursor peopleCursor = getContentResolver().query(uri, projection,
                selection, selectionArgs, sortOrder);

        if (peopleCursor != null) {

            int indexName = peopleCursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int indexId = peopleCursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);

            while (peopleCursor.moveToNext()) {

                // this is a separate activity used for my listView.  
                //It containts 2 strings (name and _id) with getters and setters for both
                ContactNameItems nameItems = new ContactNameItems();

                // Filter out no-name contacts such as auto-added email addresses from gmail
                // Compare to value of 'name' to see if they're the same, if so then pass
                if (!peopleCursor.getString(indexName).isEmpty() &&
                            !peopleCursor.getString(indexName)
                                    .equalsIgnoreCase(name) {

                    name = peopleCursor.getString(indexName);
                    contact_id = peopleCursor.getString(indexId);
                    nameCount++; //Do something with this


                    nameItems.setName(name);
                    nameItems.set_id(contact_id);

                    //Listview to add my 'nameItems'
                    mListView.add(nameItems);

                    mListAdapter.notifyDataSetChanged();
                }
            }
            peopleCursor.close();
        }
    }