Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在文本视图中显示通讯录中的联系人总数_Android_Sqlite_Textview_Android Contacts - Fatal编程技术网

Android 如何在文本视图中显示通讯录中的联系人总数

Android 如何在文本视图中显示通讯录中的联系人总数,android,sqlite,textview,android-contacts,Android,Sqlite,Textview,Android Contacts,我能够获得联系人总数,但问题是,它来自sqlite,电话簿中的联系人数与我从数据库中获得的联系人数不同。 以下是我的代码: Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); // Or this cursor Cursor cursor = managedQuery(

我能够获得联系人总数,但问题是,它来自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();
        }
我能够获得数据库中联系人的总数,但它与电话簿中显示的联系人不同


任何建议或帮助都将不胜感激

我还在学习,所以我相信还有比这更快的方法,但我只计算唯一姓名的方法是使用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();
        }
    }

你得到的可能是正确的,因为在联系人应用程序中,它不会计算加入的联系人!!!谢谢你的信息。但是,有没有其他方法可以获得准确的信息?我想,这与您传递的4个空参数有关。在链接中尝试不同的方法。