仅显示一定数量的联系人

时间:2017-12-04 10:43:09

标签: android android-recyclerview android-contentprovider android-contacts android-contentresolver

我开发了一个程序,显示电话簿中的联系人列表。 为此,我使用以下代码:

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null,
            ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'",
            null,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    if (cursor != null && cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
           Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id)); 
            Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
                    null,
                    null);
            if (phones != null) {
                    while (phones.moveToNext()) {
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        contactList.add(new Contact(name, phoneNumber, id));
                    }

                phones.close();
            }
        }
        cursor.close();
    }
    adapter = new ContactAdapter(contactList, R.layout.contacts_list_item, getApplicationContext());
    recyclerView.setAdapter(adapter);

一切正常,程序会显示我电话簿中的所有联系人,但我希望显示一定数量的联系人例如: 我打开程序并从电话簿加载前50个联系人,滚动后,接下来的50个联系人被加载到列表的末尾。等等

2 个答案:

答案 0 :(得分:1)

使用下面有限制原因的代码。用下面的代码替换你的代码

  Uniform
     Uniformity provides several benefits: it allows different types
     of resource identifiers to be used in the same context, even
     when the mechanisms used to access those resources may differ;
     it allows uniform semantic interpretation of common syntactic
     conventions across different types of resource identifiers; it
     allows introduction of new types of resource identifiers
     without interfering with the way that existing identifiers are
     used; and, it allows the identifiers to be reused in many
     different contexts, thus permitting new applications or
     protocols to leverage a pre-existing, large, and widely-used
     set of resource identifiers.

  Resource
     A resource can be anything that has identity.  Familiar
     examples include an electronic document, an image, a service
     (e.g., "today's weather report for Los Angeles"), and a
     collection of other resources.  Not all resources are network
     "retrievable"; e.g., human beings, corporations, and bound
     books in a library can also be considered resources.

     The resource is the conceptual mapping to an entity or set of
     entities, not necessarily the entity which corresponds to that
     mapping at any particular instance in time.  Thus, a resource
     can remain constant even when its content---the entities to
     which it currently corresponds---changes over time, provided
     that the conceptual mapping is not changed in the process.

  Identifier
     An identifier is an object that can act as a reference to
     something that has identity.  In the case of URI, the object is
     a sequence of characters with a restricted syntax.

它将获得前10条记录。并且在pull to refresh实现中,获取更多关于pull刷新回调事件的记录。希望这会对你有所帮助

答案 1 :(得分:-1)

我认为你想要的原因是因为加载所有联系人非常慢。 它非常慢的原因是因为您目前每个联系人有1个查询,所以如果用户有500个联系人,则需要运行500个查询。

您可以将查询数量减少到只有一个,然后我假设您不需要限制。

String[] projection = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor phones = getContentResolver().query(Phone.CONTENT_URI, projection, null, null, Phone.DISPLAY_NAME + " ASC");

while (phones.moveToNext()) {
    String name = phones.getString(1);
    String phone = phones.getString(2);
    Long id = phones.getLong(0);

    contactList.add(new Contact(name, phone, id));
}
phones.close();

如果您只想为每个联系人显示一个项目,而不是每个手机,则可以将contactList字段更改为HashMap<Long, Contact>而不是List,并添加新找到的如果现有的Contact对象已存在于地图中,则可以使用手机。

类似的东西:

Contact contact = allContacts.get(id);
if (contact == null) {
    contact = new Contact(name, phone, id);
    allContacts.put(id, contact);
} else {
    contact.addPhone(phone); // you'll need to implement this
}