gpt4 book ai didi

java - 内容提供商 ListView 限制

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:22 26 4
gpt4 key购买 nike

我想限制我的应用中显示的联系人数量。目前,它正在查询我的 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);

干杯,夏姆

最佳答案

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

关于java - 内容提供商 ListView 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29884942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com