From 51fcac04a29c1f1789cbb2414664c783d69ed940 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 27 Apr 2010 19:08:50 +0200 Subject: Use soft references to cache result lists from history. --- .../android/unicodeMap/UnicodeMapActivity.java | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/org') diff --git a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java index 687493e..e633006 100644 --- a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java +++ b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java @@ -1,5 +1,6 @@ package org.madore.android.unicodeMap; +import java.lang.ref.SoftReference; import java.util.Arrays; import java.util.List; import java.util.ArrayList; @@ -30,8 +31,18 @@ public final class UnicodeMapActivity extends ListActivity { protected static abstract class Display { protected String title; + protected SoftReference> listCacheRef; public Display(String title) { this.title = title; } public final String getTitle() { return this.title; } + public void setListCache(List list) { + this.listCacheRef = new SoftReference>(list); + } + public List getListCache() { + if ( this.listCacheRef != null ) + return this.listCacheRef.get(); + else + return null; + } } protected static class RootDisplay extends Display { public RootDisplay() { super(null); } @@ -138,12 +149,17 @@ public final class UnicodeMapActivity extends ListActivity { protected void doDisplay(Display disp, boolean isNew) { curDisp = disp; - List list; + List list = disp.getListCache(); UnicodeArrayAdapter adapter; - if ( disp instanceof RootDisplay ) { + if ( list != null ) { + android.util.Log.v("UnicodeMapActivity", "list was retrieved from its cache"); + adapter = new UnicodeArrayAdapter(this, list); + setListAdapter(adapter); + } else if ( disp instanceof RootDisplay ) { list = new ArrayList(UnicodeCharacter.Range.values().length); for ( UnicodeCharacter.Range rng : UnicodeCharacter.Range.values() ) list.add(rng); + disp.setListCache(list); adapter = new UnicodeArrayAdapter(this, list); setListAdapter(adapter); } else if ( disp instanceof RangeDisplay ) { @@ -152,6 +168,7 @@ public final class UnicodeMapActivity extends ListActivity { list = new ArrayList(db.countRange(from,to)); for ( UnicodeCharacter ch : db.getRange(from,to) ) list.add(ch); + disp.setListCache(list); adapter = new UnicodeArrayAdapter(this, list); setListAdapter(adapter); } else if ( disp instanceof SearchDisplay ) { @@ -161,6 +178,7 @@ public final class UnicodeMapActivity extends ListActivity { list = new ArrayList(sizeHint); for ( UnicodeCharacter ch : db.searchNames(s,limit+1) ) list.add(ch); + disp.setListCache(list); int size = list.size(); ((SearchDisplay)disp).setSizeHint(size); boolean overflowed = ( size > limit ); -- cgit v1.2.3