Is it normal to use AsynTask in viewHolder in CustomAdapter - android

I have custom adapter (one image, 2 text fields), and I need to check image inside viewHolder. When image is already exist, show that image in ImageView and when image isn't exist, I need to check connection, if all good, show progressbar, download image, and show it, and if all bad then show default image. Sorry for my English.

No you should not implement a Async Task inside ViewHolder bcoz you do not know how much time it will take to load images and the user will be pissed at waiting for the listview to come up.
There are lot of tutorials out there you should use lazy loading for images if you are using URLS for images if there are not much images then you can store it in database or in a file and show any time in the listview
Hope it Helps :)

I've seen many people using, it is normal for small operations.
but it can't be used for tasks which take time - as the user experience is affected by lagging.
for small operations you can use it.

Related

Android - Loading multiple text files into a RecyclerView

I'm writing a notetaking application for Android, and I have a RecyclerView to display a grid of all the notes. Each note has a respective class called Note.
The note class knows/has:
The filename (Notes title)
The file path for the note
A loading method that reads and returns the text inside the file
The recyclerview reads from a ArrayList of Note. The list is populated by a scan of files in the App's note directory, and a Note object is created for each file. I'm trying to find the best way to load the text into the recyclerview, which displays a preview of the notes. I'm wondering if theres a best way, or least stupid way to do this. The ways I can think of doing this are:
Load every note's content at once during the initial scan and save it as a string in the note class in RAM, the downside being very high RAM usage with alot of notes and text.
Load the note's text in the Recylerview's onBindViewHolder method, but I'm worried if the user is scrolling through notes very fast, and a ViewHolder and its note are unbound before the text is loaded, there could be possible issues here.
Load each note's text in a threadpool, increases performance but same issue as above.
Is there a best way to do this, or a recommended/standard way to handle alot of files loading dynamically into a recyclerview?
Start off with the first one, then optimise as needed. If theyre just text files, they probably wont take up much memory. If you find they do take up too much memory or take too long to load you can look at strategies to fix that, but dont waste time optimising something before you know its a problem
How did you tacke The problem?
I am also making a same Kind of APP, I thing The best option is The second (loading filé contents on bindViewHolder) in an async maner (using asynctask, for example), it's the same BASIC ideia of imagem loaders like Picasso or Glide, you call them on Bind ViewHolder.
If having bad performance you could wrap The async call alongside a caching strategy.

ListView with Image Adapter Locking the UI Thread

I have a listview that load images with a adapter. Every time that I scroll the screen, the listview calls the getView to refresh the listview, then the getView loads a image, doing that in my ui thread, locking my screen for a moment.
What I did was to load the images in a AsyncTask class. But the problem is that it take a little bit, and when you scroll you see the wrong image, after a while it louds the right image. I didn't want to have that problem, showing the wrong image for a moment.
I search and saw that there is no way to stop that listview automatic refresh.
What should I do to resolve that problem? Any tutorial would be helpful.
What should I do to resolve that problem?
Set the ImageView to show a placeholder image in getView(), while you fork the task to load the real image. Or, as others have suggested, use any number of libraries for managing all of that for you (and I echo the Picasso recommendation).

Using an adapter with asynchronous task

I have a class that extends BaseAdapter, which I use for a ListView. In the getView() method of that class I'm using an AsyncTask with a callback method to download an image (once downloaded, I store it, so I don't have to download it again). When the ListView loads the items first, only the first item displays an image an then starts to change the images (repeatedly displaying the images of the other items). Later also the other items start showing the same behaviour. After a while they stop cycling the images and each item shows the correct images. If I scroll the ListView the items are starting again to cycle the images.
This only happens, if I recycle the convertView parameter, that is passed to getView(). If I don't the images take very long to show up, and I'm afraid I'm creating too much new Views.
Thanks in advance for any advices, to get this working properly.
The simple way to get this going is to dump all your own image loading code and use something that already handles this, like Square's Picasso. There are plenty of alternatives if you do not like Picasso.
Otherwise, your tasks will need to be aware of the recycling, so they do not attempt to update an ImageView that now needs a different image. For example, you could use setTag() and getTag() on the ImageView to store (and retrieve) the URL the ImageView currently needs, and if the image downloaded by the task is some other URL, don't apply it.
But, again, please use an existing library for this. It's 2013: few people should be rolling their own download-the-images-for-use-in-ListView-rows code anymore.

Keep the images downloaded in the listview during scrolling

I'm developing my first app and have been reading a LOT here.
I've been trying to find a solution for the following issue for over a week with no luck.
I have an Adapter that extends ArrayAdapter to show image and 3 lines of text in each row.
Inside the getView I assign relevant information for the TextViews and use ImageLoader class to download image and assign it to the ImageView.
Everything works great! I have 4.5 rows visible on my screen (out of total of 20). When I scroll down for the first time the images continue to download and be assigned in right order to the list.
BUT when I scroll back the list looses all the images and start redrawing them again (0.5-1 sec per image) in correct order. From what I've been reading it's the standard list performance but I want to change it.
I want that, once the image was downloaded, it will be "sticked" to the list for the whole session of the current window. Just like in Contacts list or in the Market. It is only 20 images (6-9kb each).
Hope I managed to explain myself.
you need to cache each image after download it, and each time the adapter need it check if its already downloaded get it from cache (disk or memory) otherwise download it.
at first i recommended you to read this tutorial from android dev site http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
or use an external lib like this one https://github.com/koush/UrlImageViewHelper
The problem here is that the ArrayAdapter is reusing the list view rows as you scroll, so as you scroll down, the top row will be reused and inserted at the bottom of the list (for performance reasons).
Your best bet here is to try to cache the images locally on your device to avoid calling the ImageLoader every time.
One pretty good library that solves this problem is ignition. It's open source and available here: https://github.com/kaeppler/ignition
Take a look at RemoteImageView for a good example of the caching mechanism.

Android ListView with an incrementally built ListAdapter

I am trying to figure out the right architecture for a list view that loads information from an http endpoint. My problem is that the information to be displayed has both text and image content. If I only load the text content the the UI is very responsive but if I also load the image content then it takes a few seconds for the list to be populated. I parse the content as it comes in and build up a list adapter but what I want to do is build up the list adapter incrementally so that the user sees the information as it becomes available instead of waiting until the last item is processed before any information is displayed. Currently I do everything with the help of AsyncTask so I'm aware of threads and how they can be helpful so I'd like to know how other people have worked around the issue of displaying list based information as quickly as possible.
So this solution doesn't incrementally build the ListView but if the text information comes quickly as you mentioned I think it might help:
Lazy load of images in ListView
Basically this solution will place a stub image in the ImageView until the desired image is finished downloading.
It is not only about loading images as far as i understand your question. Please look at "Implementing a Dynamically Loading Adapter (Endless List)" tutorial: http://codinglines.frankiv.me/post/14552677846/android-implementing-a-dynamically-loading-adapter

Categories

Resources