3

我正在使用以下方法通过电话号码提取联系人姓名和缩略图。当它运行时,GC 会发疯。Methos 在实用程序类中,并从适配器调用以填充列表视图。列表有 20 多个项目/号码来查询联系人数据库。

我是 Java 和 Android 的新手,我似乎无法弄清楚哪里出了问题,或者如果没有问题,如何优化它。

主要问题是 listview 在 ViewPager 中,这使得水平滚动非常缓慢和生涩。

public static Contact getContact(Context ctx, String phoneNumber)
{
    String contactName = "";
    Long contactId= 0L;
    Bitmap contactPhoto =null;
    ContentResolver localContentResolver = ctx.getContentResolver();
    Cursor contactLookupCursor =  
       localContentResolver.query(
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
                Uri.encode(phoneNumber)), 
                new String[] {PhoneLookup.DISPLAY_NAME, BaseColumns._ID}, 
                null, 
                null, 
                null);
    try {
    while(contactLookupCursor.moveToNext()){
        contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
        contactId = contactLookupCursor.getLong(contactLookupCursor.getColumnIndexOrThrow(BaseColumns._ID));
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(), uri);
        contactPhoto = BitmapFactory.decodeStream(input);
        }
    }
    finally {
    contactLookupCursor.close();
    } 

    return new Contact(contactName,contactId,contactPhoto);
}

这是来自 logcat 的示例

 10-16 22:05:39.745: D/dalvikvm(29171): GC_CO

NCURRENT freed 322K, 6% free 15200K/16071K, paused 1ms+11ms
10-16 22:05:40.005: D/dalvikvm(29171): GC_FOR_ALLOC freed 1428K, 12% free 14170K/16071K, paused 15ms
10-16 22:05:40.080: D/dalvikvm(29171): GC_CONCURRENT freed 70K, 12% free 14303K/16071K, paused 1ms+8ms
10-16 22:05:40.230: D/dalvikvm(29171): GC_CONCURRENT freed 305K, 10% free 14476K/16071K, paused 2ms+1ms
10-16 22:05:40.315: D/dalvikvm(29171): GC_EXPLICIT freed 169K, 11% free 14348K/16071K, paused 4ms+12ms
10-16 22:05:40.660: D/dalvikvm(29171): GC_CONCURRENT freed 329K, 10% free 14495K/16071K, paused 1ms+3ms
10-16 22:05:40.850: D/dalvikvm(29171): GC_CONCURRENT freed 543K, 11% free 14416K/16071K, paused 1ms+1ms
10-16 22:05:40.980: D/dalvikvm(29171): GC_CONCURRENT freed 380K, 10% free 14497K/16071K, paused 1ms+4ms
10-16 22:05:41.135: D/dalvikvm(29171): GC_CONCURRENT freed 521K, 11% free 14440K/16071K, paused 2ms+2ms
10-16 22:05:41.375: D/dalvikvm(29171): GC_CONCURRENT freed 406K, 11% free 14444K/16071K, paused 1ms+2ms
10-16 22:05:41.470: D/dalvikvm(29171): GC_CONCURRENT freed 270K, 10% free 14583K/16071K, paused 2ms+2ms
10-16 22:05:41.680: D/dalvikvm(29171): GC_CONCURRENT freed 290K, 9% free 14774K/16071K, paused 2ms+4ms
10-16 22:05:41.760: D/dalvikvm(29171): GC_EXPLICIT freed 98K, 9% free 14749K/16071K, paused 3ms+2ms
10-16 22:05:41.900: D/dalvikvm(29171): GC_CONCURRENT freed 199K, 7% free 14973K/16071K, paused 1ms+3ms
10-16 22:05:42.070: D/dalvikvm(29171): GC_CONCURRENT freed 277K, 7% free 15086K/16071K, paused 1ms+3ms
10-16 22:05:42.335: D/dalvikvm(29171): GC_CONCURRENT freed 416K, 6% free 15158K/16071K, paused 1ms+3ms
10-16 22:05:42.430: D/dalvikvm(29171): GC_CONCURRENT freed 1235K, 11% free 14315K/16071K, paused 1ms+2ms
10-16 22:05:42.525: D/dalvikvm(29171): GC_CONCURRENT freed 452K, 11% free 14364K/16071K, paused 2ms+3ms
10-16 22:05:42.925: D/dalvikvm(29171): GC_CONCURRENT freed 371K, 11% free 14445K/16071K, paused 2ms+1ms
10-16 22:05:43.045: D/dalvikvm(29171): GC_CONCURRENT freed 495K, 11% free 14335K/16071K, paused 2ms+2ms
10-16 22:05:43.190: D/dalvikvm(29171): GC_CONCURRENT freed 295K, 11% free 14431K/16071K, paused 2ms+3ms
10-16 22:05:43.315: D/dalvikvm(29171): GC_EXPLICIT freed 236K, 11% free 14329K/16071K, paused 2ms+3ms
10-16 22:05:43.575: D/dalvikvm(29171): GC_CONCURRENT freed 316K, 11% free 14436K/16071K, paused 1ms+1ms
10-16 22:05:43.660: D/dalvikvm(29171): GC_CONCURRENT freed 281K, 10% free 14585K/16071K, paused 1ms+2ms
10-16 22:05:43.905: D/dalvikvm(29171): GC_CONCURRENT freed 309K, 9% free 14754K/16071K, paused 2ms+3ms
10-16 22:05:44.070: D/dalvikvm(29171): GC_CONCURRENT freed 270K, 7% free 14968K/16071K, paused 2ms+2ms
10-16 22:05:44.300: D/dalvikvm(29171): GC_CONCURRENT freed 265K, 7% free 15103K/16071K, paused 1ms+4ms
10-16 22:05:44.495: D/dalvikvm(29171): GC_CONCURRENT freed 400K, 6% free 15176K/16071K, paused 2ms+5ms
10-16 22:05:44.695: D/dalvikvm(29171): GC_CONCURRENT freed 447K, 6% free 15137K/16071K, paused 3ms+6ms
10-16 22:05:44.780: D/dalvikvm(29171): GC_EXPLICIT freed 354K, 7% free 15037K/16071K, paused 2ms+3ms
10-16 22:05:45.075: D/dalvikvm(29171): GC_CONCURRENT freed 422K, 7% free 15104K/16071K, paused 1ms+2ms
10-16 22:05:45.140: D/AbsListView(29171): Get MotionRecognitionManager
10-16 22:05:45.340: D/dalvikvm(29171): GC_CONCURRENT freed 293K, 6% free 15252K/16071K, paused 2ms+6ms
10-16 22:05:45.340: D/AbsListView(29171): [unregisterDoubleTapMotionListener]
10-16 22:05:45.340: I/MotionRecognitionManager(29171):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@41a600c8
10-16 22:05:45.530: D/dalvikvm(29171): GC_CONCURRENT freed 849K, 8% free 14799K/16071K, paused 2ms+7ms
10-16 22:05:45.720: D/dalvikvm(29171): GC_CONCURRENT freed 394K, 8% free 14908K/16071K, paused 3ms+5ms
10-16 22:05:45.940: D/dalvikvm(29171): GC_CONCURRENT freed 452K, 8% free 14845K/16071K, paused 2ms+3ms
10-16 22:05:46.085: D/dalvikvm(29171): GC_CONCURRENT freed 374K, 8% free 14917K/16071K, paused 1ms+3ms
10-16 22:05:46.335: D/dalvikvm(29171): GC_CONCURRENT freed 457K, 8% free 14892K/16071K, paused 1ms+2ms
10-16 22:05:46.390: D/dalvikvm(29171): GC_EXPLICIT freed 148K, 8% free 14870K/16071K, paused 1ms+3ms
10-16 22:05:46.550: D/dalvikvm(29171): GC_CONCURRENT freed 415K, 8% free 14844K/16071K, paused 2ms+4ms
10-16 22:05:46.680: D/dalvikvm(29171): GC_CONCURRENT freed 223K, 7% free 15046K/16071K, paused 2ms+3ms
10-16 22:05:46.825: D/AbsListView(29171): Get MotionRecognitionManager
10-16 22:05:46.965: D/dalvikvm(29171): GC_CONCURRENT freed 346K, 6% free 15124K/16071K, paused 1ms+3ms
10-16 22:05:46.965: D/AbsListView(29171): [unregisterDoubleTapMotionListener]
10-16 22:05:46.965: I/MotionRecognitionManager(29171):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@41a49d90
10-16 22:05:47.095: D/dalvikvm(29171): GC_CONCURRENT freed 755K, 9% free 14758K/16071K, paused 2ms+5ms
10-16 22:05:47.375: D/dalvikvm(29171): GC_CONCURRENT freed 416K, 8% free 14835K/16071K, paused 2ms+5ms
10-16 22:05:47.565: D/dalvikvm(29171): GC_CONCURRENT freed 415K, 8% free 14888K/16071K, paused 2ms+5ms
10-16 22:05:47.975: D/dalvikvm(29171): GC_CONCURRENT freed 475K, 8% free 14814K/16071K, paused 2ms+3ms
10-16 22:05:48.105: D/dalvikvm(29171): GC_CONCURRENT freed 411K, 8% free 14867K/16071K, paused 2ms+3ms
10-16 22:05:48.155: D/dalvikvm(29171): GC_EXPLICIT freed 114K, 9% free 14760K/16071K, paused 2ms+4ms
10-16 22:05:48.280: D/dalvikvm(29171): GC_CONCURRENT freed 284K, 8% free 14902K/16071K, paused 1ms+3ms
10-16 22:05:48.435: D/dalvikvm(29171): GC_CONCURRENT freed 332K, 7% free 15000K/16071K, paused 3ms+4ms
10-16 22:05:48.655: D/dalvikvm(29171): GC_CONCURRENT freed 415K, 7% free 14985K/16071K, paused 1ms+2ms
10-16 22:05:48.805: D/dalvikvm(29171): GC_CONCURRENT freed 451K, 7% free 15002K/16071K, paused 3ms+5ms
10-16 22:05:49.150: D/dalvikvm(29171): GC_CONCURRENT freed 417K, 7% free 14985K/16071K, paused 1ms+2ms
4

2 回答 2

3

您正在迭代整个结果集,为所有缩略图创建位图,然后将所有这些结果扔掉,除了最后一个。如果您只想要最后一个,只需处理最后一个。

于 2012-10-16T21:16:07.727 回答
2

一种选择是在 Activity 启动时进行光标查找并将缩略图和电话号码保存到一个对象中,然后在缓存中保存这些对象的列表。

然后,当您显示 ListView 时,您可以从本地缓存中读取,而不是每次都为每个项目进行数据库查找。


另外,创建一个返回所有联系人的查询,然后查询此游标结果以获取电话号码并因此查询该行不是更好吗?

于 2012-10-16T21:13:09.580 回答