如何从android中联系10 -10个联系人

时间:2013-12-10 17:35:26

标签: 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个数字)数字列表

2 个答案:

答案 0 :(得分:1)

我会选择LIMIT功能。

SELECT * FROM table LIMIT 10 - &gt;得到前10条记录

SELECT * FROM table LIMIT 10, 10 - &gt;第10行后获得10条记录

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

希望这有帮助。

答案 1 :(得分:0)

正确的方法是在查询中添加LIMIT和OFFSET限定符。示例:

select * from MyTable where <whatever> limit 10 offset 30;

这将获得30-39行。

你是否可以通过cr.query()调用来完成这项工作,我不知道。