首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android中获取10 -10联系人

如何在android中获取10 -10联系人
EN

Stack Overflow用户
提问于 2013-12-10 17:35:26
回答 2查看 1.4K关注 0票数 1

在我的应用程序中,我只需要获取10个联系人,接下来的10个联系人将在我按下next按钮时获取,等等。

代码语言:javascript
复制
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 )联系人号,当我单击next按钮时,它应该会获取next (10个号码)的号码列表。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-10 17:49:55

我会选择极限函数。

SELECT * FROM table LIMIT 10

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

代码语言:javascript
复制
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "LIMIT 10, " + count);
count += 10;

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2013-12-10 17:49:46

正确的方法是向查询中添加限制和偏移量限定符。示例:

代码语言:javascript
复制
select * from MyTable where <whatever> limit 10 offset 30;

这将得到第30-39行。

我不知道您是否可以通过cr.query()调用来完成这个任务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20501375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档