Android: why Thread in getView () is not working?

I would expect this (inside getView() ) to work ...

but nothing happens, bitmaps do not load in ImageView ....

 Thread th= new Thread(new Runnable() { @Override public void run() { ImageLoader.getInstance().displayImage(rss.getChannel().getItems().get(position).getEnclosure().getUrl(),holder.imageView,options,new ImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { //...stuff... } //... more stuff @Override public void onLoadingCancelled(String imageUri, View view) { //...stuff } }); } }); th.start(); 

Please, why is this not so?

Thanks: -)

0
source share
3 answers

What you are trying to do is a really bad practice. However, I can’t tell you the information that you present to us, what is the problem. I think the ImageLoading library is already doing something in its thread, so your Thread is already finished. In addition, only the main user interface thread can access or manipulate UI views, but an exception will be thrown if this is a problem.

In addition, I would recommend you use Picasso . It handles threadpooling, scheduling, caching, memory management (avoid memory leaks with WeakReference) and revises the view in a ListView or RecyclerView.

Update:

You create Thread every time getView() is called while scrolling. This is bad practice because creating a Thread expensive. Consider dropping or scrolling through the list quickly. If you scroll through 20 items, 20 threads will be created and started. All of them have the same priority as the main user interface thread. This means that the CPU is shared between 20 threads and the main thread of the user interface. This can lead to poor user interface performance. You must use ThreadPool . In addition, you do not stop your topic. This leads to two problems:

  • Your view gets recycled in getView() , but Thread doesn’t, so the thread is still working while another view should be displayed in the view.
  • You get memory leaks! Do you know how the garbage collector works? It starts at the thread level and searches for unused objects (objects that no other objects refer to anymore). Your topic is an inner class. Therefore, it has a solid reference to the surrounding Adapter class. Adapter attached to ListView , ListView to Activity . Therefore, if your activity is closed by the user, the garbage collector cannot collect Activity and all views and other objects until Thread (Thread) is complete. You probably don't have enough memory.

And last, but not least, the Android system has its own message streaming system, and only the main user interface thread can access the user interface views. I do not want to understand in detail here, I am sure that you can do this. At this point, I recommend using AsyncTask if you really need to start your own Thread. But you also have to manually cancel AsyncTask to avoid memory leaks.

+4
source

First of all, the user interface of the Android application is not updated and does not access the workflow. This way your stream will not change / will not reach the image.

Secondly, you do not need Thread to download the image, since the ImageLoader library itself will take care of this.

+1
source

Only UIThread can change its views. I suggest using the runOnUIThread() method from Activity .

Additional information: https://developer.android.com/training/multiple-threads/communicate-ui.html

+1
source

Source: https://habr.com/ru/post/1213334/


All Articles