How does android display a list with 200 elements? - android

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.

Related

How to further optimize android recyclerview?

In the beginning we had listViews. As a developer, we had to recycle and reuse the views to have a fluid experience.
Then, came the recylclerviews. Now, all the recycling heavy lifting is managed by android library itself.
Using pagination, an infinite recycler view can be implemented, which loads the data when needed.
But there is one problem I am still facing in infinite recyclerview. How is the data in the adapter managed?
In most of the infinite scroll implementations of recyclerview, the new data is appended to the original data. This makes the size of data set ever increasing.
Why cant dataset itself behave like recyclerview and recycle its data, instead of appending? (Like a circular queue).
How can one manage the positions of itemviews, when the dataset is a circular queue. Is it unnecessary and yields too little performance improvement? Am I missing some design pattern?
It would be possible to clear data already loaded, but this way you have to load data not only for "bottom" views, but for "upper ones" too. So user wants to back to previous data and he needs to load that data again, that's the problem.
There could be more work about notifying data set changed in recyclerView.
You can implement it this way, but remember about pagination and how it is implemented - for example limiting data in SQL statements standard way can output different data each call, so user backing to previous data can see other results! Check Twitter pagination for one of the way to achieve same results each time.
Pros:
less data in memory
Cons:
more work to write the code
more data loading for users (more internet usage, more loading times - when users wants back to previous data)
pagination has to be designed more carefully

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.

Loading new data to android lists - how it should be (from 1.5 to 4.1)

Today I was thinking about how Android framework works, and I'm not sure about one thing - how developer (me) should program lists to show custom data from few sources. I created few apps, but its important to me to keep my applications clean and fast as possible.
So - I have an app, for example news reader. Im using Fragments, ListFragments, custom layouts for list items and BaseAdapter for showing data in many lists and activities. Nothing new here, and nothing special. At first I download all data from webserver to sqlite in app, so Im sure about it speed and stability. And then what's next?
Should i create List items from Java Lists (List items) and then pass it to baseadapter, or I should only use way SQLite Cursor->list item? What is better to refresh list, add new items and delete them? Remember that i have my ListFragment with baseadapter in separate class.
Im not sure about it, so I clearly dont know how should i refresh and load new items to my list. I just want to keep app clean, without many 3rd party libraries (so i dont care about EndlessList or something like that).
tl;dr
How should I create list for speed purposes, when i use few sources to show data?
I'm not sure whether I understand your full question but about the part which data source to use for an Adapter, here are my thoughts:
I think that depends on the amount of data you are dealing with. If it is sufficiently small you can keep everything in a List in memory, but you also need to be sure that the data won't grow over time.
Using a Cursor as a data source is unfortunately also not a safe option for getting around a possible "Out Of Memory" exception problem. There is a limit per query because of the implementation of the cursor which apparently loads the whole result set into memory.
So if you are dealing with a lot of data or data that potentially grows over time (like messages, user-created items), you need to have an Adapter that internally works with a data window which loads a fixed amount of items into memory at a time. That window always keeps a bunch of items in memory which can be currently viewed and quickly reached with the ListView (by scrolling up and down).
Let's say that window holds for example 200 items.
When the upper / lower bound of that window is reached the Adapter needs to load the next adjacent window. You can observe that kind of behavior in the GMail app ("Loading conversations....").
I would let that data sit in the cursor and not transfer it to a list first, because you can use the cursor like a list.

How many items a ListView can store?

I'm new to Android programming. I wonder how many items a ListView can store? I search in docs but they don't talk about this. What if I put a lot (maybe 10k) items into a ListAdapter, will it affect to the performance?
Cheers,
MK.
The ListView is virtualized in Android. Practically speaking, that means that there's no actual limit for the number of elements inside it. You can put millions of rows inside the list and it'll only allocate memory for the currently visible ones (or a few more tops).
Check out the dozens of tutorials regarding writing a custom Adapter class for an AdapterView (ListView extends that). Also check out the Google I/O 2010 session on ListViews; it's really useful: here
There's no limit as the ListView only renders items when they come into view, and so only cares about the data for the ListView when it comes to render the item (though it needs to know the quantity of items to render the scrollbar correctly)
The Google IO video really is great for learning about ListView
http://www.youtube.com/watch?v=wDBM6wVEO70
That said, I'd ask whether you SHOULD load that many, as clearly the user cannot look at them all and scrolling around a ListView with that many items will be very tedious. If it was me I'd be asking some questions:
Does the list need to show them ALL initially? Can it just show the most relevant set? The nearest/biggest/smallest/best/etc
Rather than load them all at once, can you load blocks in pages of items? So for example you load 10-100 initially, and when the user gets to the bottom show "Loading more..." with a progress spinning icon, and pull more in, then the user can choose how many to load, and how much scrolling they are prepared to do
Should you be building a UI to filter the items so there's never a need for them to look at 10,000?
More on ListView
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
http://developer.android.com/guide/topics/ui/binding.html
http://www.androidguys.com/2008/07/14/fancy-listviews-part-one/
Integer.MAX_VALUE since most of its functions will break as they rely on int position.
I created a ListView and used Integer.MAX_VALUE as the number returned to the adapter for the number of items in the list. This had no effect on the performance of the ListView even though it was holding about 2 billion items.
I think 10k will work :P
Check out my answer here to see in detail what I mean : How to create a closed (circular) ListView?
You have a memory limit (which is device specific). As long as you don't exhaust your memory limit, you can store as many items as you wish. There are ADT tools that you can use to monitor how much memory your application is using at any moment.

Large ListView in Android

Building a dictionary application I need a ListView that shows over 100k items. I want to let the user scroll as much as needed.
What's the best practice for searching and showing words?
Is it ok to show 150,000 words in the ListView (for pereformance)?
If not how to add 100 other words after the user reaches to the end of the list?
Currently I show 50 words previous and 50 words next of searched word.
Thank you.
(second answer in response to clarification about performance)
There are different ways to do this based on where your data is.
The best way is to have your data in a sqlite database and use a CursorAdapter. Android then manages the fetching of your data and won't fetch data that isn't currently being shown on the screen.
If your words are in an array in memory, try ArrayAdapter or SimpleAdapter.
The Adapter interface, from which all of the above classes inherit, is designed to provide good ListView performance regardless of the number of objects in the list.
One built-in way to allow fast scrolling is via the fast scroll thumb. In your xml, set:
android:fastScrollEnabled="true"
on your ListView, or try:
listView.setFastScrollEnabled(true)
listView.setFastScrollAlwaysVisible(true)
RecyclerView is very good for this. I have developed an open source library especially designed to scroll through large list with extreme speed. In fact it can move well in excess of 1000 items per second both in single and multiple column layouts You can check out the repo here: https://bitbucket.org/warwick/hgfastlist or you can checkout the OpenGL version here: https://bitbucket.org/warwick/hgglfastlist. Here is a demo video on Youtube: https://www.youtube.com/watch?v=oz7aeAlOHBA&feature=youtu.be
Even if you don't want to use this library, there's loads of code to read through in the demo app that will give you good ideas.

Categories

Resources