0

I came across an early post regarding lazy loading image in listview, and tried to experiment with a posted answer(the 1st answer with a green check). Lazy load of images in ListView.

However i am still confused with 2 things:

  1. what is the definition of 'lazy loading' in a list view? From what i thought before on UI perspective, if the item images are stored locally in phone, 'lazy loading' means you do not start to load the list item until user can see or scroll to that item (when items are more than 1 screen). But here, with image stored in remote url, it seems to means that 'an image is loaded from url 1st time, afterwards will load from local cache', is this the idea of 'lazy' here?

  2. in the 1st answer in the post, the DrawableManager.fetchDrawable() method can not be used in the getView() method of listview adapter directly, it seems. I am getting below error msg from ddms log (see below), and it's saying sth about 'NetworkOnMainThreadException'. So how can DrawableManager.fetchDrawable() be use in listview here? is it true that only a thread based implementation like DrawableManager.fetchDrawableOnThread() can be used in a list adapter's getView() function?

Could you share some thoughts on these question? Appreciate it.

10-25 04:52:43.628: E/AndroidRuntime(2231): FATAL EXCEPTION: main
10-25 04:52:43.628: E/AndroidRuntime(2231): android.os.NetworkOnMainThreadException
10-25 04:52:43.628: E/AndroidRuntime(2231):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at com.example.fairfax.MainActivity$DrawableManager.fetch(MainActivity.java:230)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at com.example.fairfax.MainActivity$DrawableManager.fetchDrawable(MainActivity.java:187)
10-25 04:52:43.628: E/AndroidRuntime(2231):     at com.example.fairfax.MainActivity$LazyAdapter.getView(MainActivity.java:162)
Community
  • 1
  • 1
user1559625
  • 2,583
  • 5
  • 37
  • 75

2 Answers2

0
  1. lazy loading in general means that the resource is not loaded until it is really needed. in your example that means exactly what you said: as soon as the list item is shown the first time, the images gets loaded from the URL. the next time it can be loaded from an internal cache. Be careful though with the cache to not use too much memory.
  2. Network access should never happen in the UI thread, otherwise the UI will freeze for the time it takes to load the resource. I don't know anything about DrawableManager.fetchDrawableOnThread(), but I usually use an AsyncTaks for that. This helps to keep the UI responsive. Just google for "android async image loading" or something similar, you will find plenty of examples.
SimonSays
  • 10,867
  • 7
  • 44
  • 59
0

You should call fetchDrawableOnThread method and pass your url and imageview as params, because by calling this method you are giving image downloading task to a non ui thread which then sends message by using Handler to update ui.

maninder singh
  • 1,286
  • 13
  • 16