【问题标题】:Content Provider ListView Limit内容提供者 ListView 限制
【发布时间】:2015-04-26 23:36:31
【问题描述】:

我想限制在我的应用中显示的联系人数量。目前它正在查询我的 Contactscontract.Contacts 数据库并返回每个具有电话号码的主要显示名称。有没有一种简单的方法可以将其减少到一个数值(比如只显示 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);

干杯, 夏姆

【问题讨论】:

  • 答案是否可能依赖于创建一个数组列表,然后 applybatch() 删除所有不需要的项目?

标签: java android android-listview android-fragmentactivity


【解决方案1】:

要实现查询结果的限制,请在您的查询中添加(字符串连接)一个“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);
...

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
相关资源
最近更新 更多