内容提供商ListView限制

时间:2015-04-26 23:36:32

标签: java android android-listview android-fragmentactivity

我想限制我的应用中显示的联系人数量。目前,它正在查询我的Contactscontract.Contacts DB并返回每个具有电话号码的主显示名称。有没有一种简单的方法可以将其减少到一个数字量(比如只显示5个联系人),或某些指定的ID?

这是我到目前为止所做的:

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    // load from the "Contacts table"
    Uri contentUri = ContactsContract.Contacts.CONTENT_URI;

    // no sub-selection, no sort order, simply every row
    // projection says we want just the _id and the name column
    return new CursorLoader(getActivity(),
            contentUri,
            PROJECTION,
            ContactsContract.Contacts.HAS_PHONE_NUMBER + " =?", // This is selection string, were looking for records that HAS_PHONE_NUMER is 1
            new String[]{"1"}, // 1 means that contact has a phone number
            ContactsContract.Contacts._COUNT,
            new String[] {"5"},
            null);
}

每当我尝试在返回部分添加新参数时,Android Studio会立即显示红色,表示无法解析构造函数。这是因为CursorLoader没有定义为接收更多参数吗?

我之前在代码中将其定义为:

mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);

干杯, 希亚姆

1 个答案:

答案 0 :(得分:0)

要实现查询结果的限制,请在查询中添加(字符串连接)“LIMIT 5”:

...
ContactsContract.Contacts.HAS_PHONE_NUMBER + "=? LIMIT 5" //This is selection string, were looking for records that HAS_PHONE_NUMER is 1 AND 
// the result set is limited to 5 rows
new String[]{"1"},
null);
...