开发者

Retrieving contact info in Android fails with exception

开发者 https://www.devze.com 2023-04-09 09:05 出处:网络
ALL, I am trying to execute following piece of code: ContentResolver cr = getContentResolver(); Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, \"ContactsContract.Contacts.DISPLAY_

ALL, I am trying to execute following piece of code:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, "ContactsContract.Contacts.DISPLAY_NAME LIKE '" + name + "'", null, null );

However when running this code, I am getting SQLite exception: "Don't know such file ContactsContract.Contacts.DISPLAY_NAME:, while compiling "....."".

The problem is that I don't know in advance what wi开发者_如何学运维ll "name" contain, hence using "LIKE" clause.

Is there a better way to perform such operation? Or I am just doing it incorrectly?

Thank you in advance for any help.


Or I am just doing it incorrectly?

ContactsContract.Contacts.DISPLAY_NAME is a Java construct. Use:

Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.DISPLAY_NAME + " LIKE '" + name + "'", null, null );

Or, use positional parameters:

Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", args, null );

where args is a one-element string array containing your name.

0

精彩评论

暂无评论...
验证码 换一张
取 消