get contacts performance issue

≡放荡痞女 提交于 2019-12-05 10:36:17

You can get name and ID at the same time you get the phone number:

String[] PROJECTION=new String[] {Contacts._ID, Contacts.DISPLAY_NAME, Phone.NUMBER};
Cursor pCur = cr.query(Phone.CONTENT_URI, PROJECTION, Phone.CONTACT_ID +" = ?",
                                      new String[]{id}, null);

This eliminates the per-row sub-query.

Using a Cursor is a great approach for this problem. To get retrieve information from a specific position, you can use Cursor.moveToPosition(int position). Then you can access any field of the Cursor, in this case a String, using Cursor.getString(int columnIndex).

Take a look at the Cursor documentation for more details.

The best part about this approach is that you can implement a CursorLoader to do the background data retrieval for you. It also automatically handles repopulating your ListView on data changes, orientation changes, etc. Check out this great tutorial for more explanation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!