我想获得用户的联系人,然后附加某种常规EX pression,并把它们添加到列表视图。我目前能得到的所有联系人通过
I would like to get user contacts and then append some kind of regular expression and append them to a list view. I am currently able to get all the contacts via
getContentResolver()查询(People.CONTENT_URI,NULL,NULL,NULL,NULL);
,然后将它们传递给扩展 SimpleCursorAdapter
。
and then pass them to a custom class that extends SimpleCursorAdapter
.
所以,我想知道如何获得只匹配常规的前pression,而不是所有的用户联系人的联系人。
So I would like to know how to get only the contacts that match a regular expression and not all of users contacts.
getContentResolver().query(People.CONTENT_URI, null, null, null, null);
您应该使用类似
final ContentResolver resolver = getContentResolver();
final String[] projection = { People._ID, People.NAME, People.NUMBER };
final String sa1 = "%A%"; // contains an "A"
cursor = resolver.query(People.CONTENT_URI, projection, People.NAME + " LIKE ?",
new String[] { sa1 }, null);
这将使用参数请求(使用 ),并提供实际的值不同的参数,这样就避免了级联和prevents SQL 注射为主,如果你是请求来自用户的过滤器。例如,如果您使用的是
this uses a parameterized request (using ?) and provides the actual values as a different argument, this avoids concatenation and prevents SQL injection mainly if you are requesting the filter from the user. For example if you are using
cursor = resolver.query(People.CONTENT_URI, projection,
People.NAME + " = '" + name + "'",
new String[] { sa1 }, null);
想象一下,如果
imagine if
name = "Donald Duck' OR name = 'Mickey Mouse") // notice the " and '
和你串联的字符串。