内容提供商 ListView 限制
Content Provider ListView Limit
我想限制在我的应用程序中显示的联系人数量。目前它正在查询我的 Contactscontract.Contacts 数据库并 returning 每个具有 phone 数字的主要显示名称。有没有一种简单的方法可以将其减少到一个数值(比如只显示 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);
}
每当我尝试在 return 部分添加新参数时,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);
...
我想限制在我的应用程序中显示的联系人数量。目前它正在查询我的 Contactscontract.Contacts 数据库并 returning 每个具有 phone 数字的主要显示名称。有没有一种简单的方法可以将其减少到一个数值(比如只显示 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);
}
每当我尝试在 return 部分添加新参数时,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);
...