【发布时间】:2026-02-21 19:40:01
【问题描述】:
我的应用程序存在很大的性能问题。通过 traceview 后,我发现我的应用程序的大部分性能都被游标消耗了。所以我想知道是否有任何替代光标来处理设备联系人列表。如果别无选择,请告诉我如何处理光标,以免拖慢您的应用程序。
请帮忙!!
谢谢。
我的这部分代码有性能问题:-
public void getDisplayName()
{
Cursor c1 = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String personName = null, number = null;
try
{
Log.e(TAG, "I am here!!");
if(c1.getCount() > 0)
{
Log.e(TAG, "I am here2!!");
while(c1.moveToNext())
{
HashMap<String,String> item = new HashMap<String,String>();
String id = c1.getString(c1.getColumnIndex(Contacts._ID));
personName = c1.getString(c1.getColumnIndex(Contacts.DISPLAY_NAME));
item.put("Name", personName);
Cursor cur = this.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while(cur.moveToNext())
{
Log.e(TAG, "I am here!!3");
number = cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
item.put("Number", number);
}
displayName.add(item);
}
}
}
finally
{
c1.close();
}
}
【问题讨论】:
-
据我所知,没有其他好方法可以做到这一点。不过,我还没有遇到游标的性能问题。也许那里有一些不必要的代码?贴出来,说不定有人会发现问题。
-
向我们展示代码段落,您找到了性能问题。
标签: android performance cursor