2011-11-19 43 views
1

這是對我之前的問題(Android memory leak?)進行一些研究並遵循一些建議的步驟後的後續。Android ListView與SimpleAdapter泄漏內存

我的應用程序有一個UI循環,其中用戶單擊一個ListView項目,在查詢Web API後繪製另一個ListView。點擊一個項目將繪製另一個ListView。用戶可以多次重複該過程而無需完成活動。

這裏是我如何填充與SimpleAdapter一個ListView的代碼片斷..

//* Add available times value using array.length() 
     List<Map<String,Object>> datalist = new ArrayList<Map<String,Object>>(); 
     for (SomeResult result : results) { 
      Map map = new HashMap(); 
      map.put("field_a", result.field_a); 
      map.put("field_b", result.field_b); 
      datalist.add(map); 
     } 

     String[] from = {"field_a","field_b"}; 

     int[] to = {R.id.textview_a,R.id.textview_b}; 

     // get a reference to the ListView 

     ListView lv = (ListView)findViewById(R.id.listview); 

     lv.setBackgroundColor(0xffffffff); 

     lv.setOnItemClickListener(itemclick_drawHours); 

     SimpleAdapter adapter = new SimpleAdapter(this.getApplicationContext(), datalist, R.layout.list_item, from, to); 
     lv.setAdapter(adapter); 

     lv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
     lv.setCacheColorHint(0xffffffff); 
     viewFlipper.setDisplayedChild(2); 

使用DDMS和MAT,我注意到,雖然ListView控件對象的數量不增加,TextViews的數量, RelativeLayouts和ImageViews隨着用戶點擊次數和繪製新列表的次數不斷增加。以下是這三個小部件+ ListView在應用程序使用的各個階段的對象計數。

App Installed onto emulator: 
    android.widget.TextView: 16 
    android.widget.RelativeLayout: 9 
    android.widget.ImageView: 8 
    android.widget.ListView: 4 

Light Use (a dozen or so clicks and ListViews drawn): 
    android.widget.TextView: 107 
    android.widget.RelativeLayout: 48 
    android.widget.ImageView: 34 
    android.widget.ListView: 4 

Medium Use (a couple dozen clicks/ListViews drawn): 
    // At this point, a lot of GC messages and Resizing JIT Table messages etc. in LogCat 
    android.widget.TextView: 158 
    android.widget.RelativeLayout: 72 
    android.widget.ImageView: 61 
    android.widget.ListView: 4 

Heavy Use (three dozen or so clicks/ListViews drawn): 
    // Even more messages in logcat 
    android.widget.TextView: 222 
    android.widget.RelativeLayout: 103 
    android.widget.ImageView: 92 
    android.widget.ListView: 4 

該應用程序實際上並沒有崩潰,只是響應性和延遲性較差。經過一段時間的中等/大量使用後,關閉和打開應用程序會在顯示ImageView時導致OutOfMemory錯誤。

這裏是list_item.xml ..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<TextView 
    android:id="@+id/textview_a" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:textColor="#000000" 
    android:layout_alignParentLeft="true" 
    android:textSize="18dp" > 
</TextView> 
<ImageView 
    android:id="@+id/rightarrow" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="5dip" 
    android:src="@drawable/garrow" 
    android:layout_alignParentRight="true" 
/> 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:text="" android:id="@+id/textview_b" android:visibility="gone" /> 
</RelativeLayout> 

如果你看一下2(TextViews)至1(ImageView的)到1(RelativeLayout的)比在此模板,並在上述對象計數,似乎SimpleAdapter創建的ImageViews,TextViews和RelativeLayouts沒有正確釋放。

有什麼明顯的我失蹤了嗎?我讀過一些其他帖子,他們說這個問題似乎只發生在「調試」模式。我不知道如何打開/關閉,除了在清單中更改android:debuggable =「true」或「false」。

回答

-1
List<Map<String,Object>> ?...List of Maps each of which keys on a string to an object ... 

結果是什麼?聽起來可能就像它的鍵 - 時間就像事件的時間 - 然後你添加到一個字符串文字鍵集合 - 需要閱讀更多的文檔..

list_item.xml初始化程序=它不是一個編碼上下文程序

我在Android的非常新的,但我一直在寫Java的不夠好,看你需要填充從收集,ListView控件未經細節可能是

TreeMap<Integer,SomeResult>Stuff=new TreeMap<Integer,SomeResult>(); 

得到新的項目,程序可以找到它,你把它放在你選擇的集合中 - 它們有一個稀疏的集合數據類型工作

我沒有看到泄漏是從哪裏來的,但我敢打賭它是來自R.作爲閱讀如何工作使我可能在腦海中的候選人根據您使用它的方式相比,它如何工作