Efficient ListView in android - android

What is the best way of constructing a ListView that uses the least memory possible? This is important, because I met a few implementations and most of them is lagging when I scroll the ListView on low-end devices, but I saw a few apps, where the scroll is very smooth, even on low-end devices. How can it be done? What is the most efficient way from a memory usage point of view to construct such a ListView?

recycle your views in getView()
use ViewHolder pattern
use lazy loading if you have a lot of data to fill the list with
use Cursor as underlying data instead of object list built from cursor if your data comes from database, you save memory by not creating additional objects.
see http://www.google.com/events/io/2010/sessions/world-of-listview-android.html
see http://android.amberfog.com/?p=296

You will have to use the ViewHolder pattern.

Look at this code with description to increase the efficiency of lisview.
Keep in mind when you have lots of data to show in listview then at a time do not load all data . First load 2o data then load another when listview reaches at end.
This is also another way to increase the efficiency of listview.

Related

Optimized List view with 1k object list

I was wondering if there is any optimized way to create an adapter that holds more than 1k object in it.
I have tried the following but still the results were not promising .
A.View holder pattern-It helped but when the object size increased it started have issues
B.Note: I could have used a paginated api which passes a defined number of objects but I annot have paginated Api in my scenerio.
C.My list do have images in it but I have taken care of that.Its not a problem now.
Let me know of any new ways to achieve a adapter that can have a large number of object list in it without any issues.
As far as I know, I use some structure below
Use ViewHolder
Do not create new object if you not use
Optimizing Layout Hierarchies
https://developer.android.com/training/improving-layouts/optimizing-layout.html
Prevent Overdraw
https://developer.android.com/tools/performance/debug-gpu-overdraw/index.html
Load ImageView in ListView: I am using "Universal Image Loader" and enable mode: stop load image when scrolling
Optimize code in the getView(...) function of ListView Adapter: Try to make the code clear and short, restrictive use if{}else{}
If you have a problem with OutOfMemory, take a look at Java Reference
https://www.rallydev.com/blog/engineering/java-references-strong-soft-weak-phantom
Try to use RecycleView, it much more powerful, flexible and a major enhancement over ListView
https://stackoverflow.com/a/31199564/5381331

Is it possible to sort data in a textview?

I am using 'Codeofaninja' .
android table scroll code
He generates data. I replaced that with an sqlite database. It works well but...
I need to sort and the displays are textviews with tablerows.
Should I use listviews instead of the textviews/tablerows?
I have seen examples of data being sorted in a collection. I have my data in lists already but I have read that textviews have performance problems.
If the answer is listviews then I have to redesign the views which I am trying to not do. But if technology says I must then so be it.
So I have come up with 2 options:
1:textview gets repopulated with list after any data actions.
2:listview is where data is manipulated then stored back to db. Then I need to put a listview in the relativelayout view?
I have tried deleting the tablerows from the textview and reading data back in but this proves slow.
I searched on textviews and listviews and have seen many examples but it is still not clear as to what method is the preferred.
Thank you for input.
The idea is that the sorting is independent of the view. You sort the data in the collection (list, array, etc.) first, then use the ListAdapter (or ArrayAdapter, .etc) to populate the view.
From what you described, it seems the textviews are re-created every time, i.e, if you have 10 rows, each row has 3 textviews, did you create 30 textviews? In that case, sure it has performance problem. Try reading up on ViewHolder for ListView
Android Viewholder implementation
It is superior to use the idea of loading some data at a time. Both Android ListView and RecyclerView virtually loads data when required, and removes data when they are no longer needed.
One good tutorial about ListView # Populating a ListView With Data. Tell us what you think of it.
The only drawback for these GUI classes is when you only have small amount of data to show, which is not likely, from your post.

Android ScrollView VS ListView show many dynamic items

There is object array received from server
there are two ways to show items :
Adapt Objects To ListView
Use Scroll View
So If I use scroll View and add items programmatically, There are some Questions:
Does Adding Items Programmatically Cause Out Of Memory ?
Does It Need To Remove Items from memory ? or it will remove automatic after onDestroy Called or Items moved out of screen?
Regards
If the data set is large, a ListView won't stop OOMs by itself, you will have to avoid loading the entire data set in memory by using a CusorAdapter backed by some non-memory data store (like a sql database).
A ListView will reduce the amount of memory that the Views use to represent the data to the user, since it will only initialise Views that the user can see (and recycle Views if possible when the user scrolls).
Since only the Views visible are loaded with a ListView, performance should* also be better since the layout process will be quicker.
In most cases, a ListView would be better for this type of thing.
(*) If the adapter's getView or bindView e.t.c. are complex, or each row in the list has its own view type, the scrolling performance could actually be worse.
Listview is the Best case for you. and I will prefer to use volly library of android . It is easy to use and very handy and helpfull.

What is the purpose of Cursor Adapter?

Now, I used ListView and ArrayAdapter to show data from sqlite. My old implementation is retrieve from db and set to arrayAdapter. Then set adapter in listview.
But now considering to move to efficient adapter. What if sqlite have thousands of records ? I knew I have to retrieve first 20 records then retrieve next items based on scroll. I wonder Can I get that implementation by using cursor adapter ? Or May I know better solution for that situation.
When having thousands of records in a Database/Server response or whatever you are fetching the information from, the best practice is to do use "Lazy Loading" technique, basically what it does is, showing chunks of data sets to the user for example, 50 elements at the time, so, there's no need to load 950 elements that might not even be used, this technique improves considerably the application response and your memory too, now how you implement it is really up to you, there's a BaseAdapter class to do it on your own, or a good implementation of Cursor/Array adapters might do the trick as well.
Is important to mention that cursors have one advantage:
Cursor lets Android manage resources more efficiently by retrieving
and releasing row and column values on demand.
Hope it helps!
Regards!
First of all, the main reason I use CursorAdapter is that the content will automatically change if the data in sqlite changes. I don't need to consider about refresh UI if something changes below.
Secondly, both the query of sqlite and listview already make some optimization you want. The sqlite will not give all the data on the moment of executing query by default. It uses a slide window to gradually give the query result. And listview also does not draw all the item at once, it draws the items as the user scrolls down.
Hope this helps, though doesn't exactly answer your question
you can involve a thread which retrieve data from database and put into a Map or List. then when you need data like per 20 records when you scroll read from Map. it may help you.
just make another layer between database and view.

How does android display a list with 200 elements?

When I have a ListActivity and an Adapter how does Android handle a list with 200 elements.
Does it try to load all of them directly how does it wait till the user scrolls and then renders those elements?
Do I have to worry with performance when a list is too long?
Depends how are the adapters implemented.
If you have an adapter that is not subclassed (you use one that is provider by SDK) Android will try to load all of them directly.
I recommend to subclass SimpleCursorAdapter and implement your custom adapter. This way you will have for example 10 views (as many your screen needs), and the view it will be reused for the rest of the 190 records.
There are several parts to this question. First of all, the data itself. Is that coming from a SQLite database via a query? If so, you have a Cursor object, which contains the entire result. So if you have a query that yields 200 rows, you will have all 200 rows in memory (which is why it's so important to narrow your projection).
As for the list itself, that part is pretty efficient - Android will only create views for the elements that you can actually see. And, depending on what kind of views you have, and whether they support recycling, Android will actually recycle existing objects to minimize the amount of overhead for initialization and memory management.
I'm not sure how Android handles it internally. But most programs I've seen handle the issue by loading 20 or so items and then making the last item say "Load next 20 items". Then when you click it, it loads the next 20 items.

Categories

Resources