android - 如何从android中的联系人获取10 -10个联系人

标签 android android-contacts

在我的应用程序中,我只需要获取 10 个联系人,当我按下下一步按钮时,将获取接下来的 10 个联系人......

protected String doInBackground(String... args) {
        i = 0;
        count = 0;
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);

        names = new String[cur.getCount()];
        phone = new String[cur.getCount()];

        if ((cur.getCount() > 0)) {

            if (count < 5) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    if (Integer
                            .parseInt(cur.getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        Cursor pCur = cr
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?",
                                        new String[] { id }, null);

                        while (pCur.moveToNext() && i < 5) {
                            String phoneNo = pCur
                                    .getString(pCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            names[i] = name;
                            phone[i] = phoneNo;
                            if (i == 0)
                                fulldata += "\"" + i + "\"" + ":" + "{"
                                        + "\"" + "phone" + "\"" + ":"
                                        + "\"" + phoneNo + "\"" + "}";
                            else
                                fulldata += "," + "\"" + i + "\"" + ":"
                                        + "{" + "\"" + "phone" + "\"" + ":"
                                        + "\"" + phoneNo + "\"" + "}";
                            i++;
                        }
                        pCur.close();
                    }

                    count++;
                }
            }
        }
        return null;
    }

    // {_action:addcontacts,contacts:{"0":{"phone":"8098098"},"1":{"phone":"8098090"},"2":{"phone":"8096879"}}}
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        fulldata += "}";
        Log.i("all data to post", "" + fulldata);
        new UpdateContacts().execute();
    }
}

我正在从此代码块中获取所有联系人,但我需要获取列表中特定(10)个联系人,当我点击下一步按钮时,它应该获取下一个(10 个数字)列表

最佳答案

我会选择 LIMIT 函数。

SELECT * FROM table LIMIT 10 --> 获取前 10 条记录

SELECT * FROM table LIMIT 10, 10 --> 在第 10 行之后获取 10 条记录

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "LIMIT 10, " + count);
count += 10;

希望这对您有所帮助。

关于android - 如何从android中的联系人获取10 -10个联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501375/

相关文章:

java - 应用程序在尝试读取联系人时挂起

android - 如何在android中以编程方式将铃声添加到 native 应用程序联系人

android - 在 Wifi 到 3G 或 3G 到 Wifi 之间切换后,Web 服务调用不起作用

java - 改进如何在 GET 中传递位置

java - 当与另一个物体碰撞时旋转/旋转物体

android - 如何在android中显示所有消息选项的对话框?

android - 查询联系人的组织,但获取地址类型和值

android - 仅显示一定数量的联系人

java - 如何在 Android 上获取有关显示设备的 PCI 信息

android - 使用 PdfDocument 将 HTML 转为 PDF