What is the purpose of Cursor Adapter? - android

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.

Related

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 Adapt Data from a SQLite Database to a Listview

EDIT: After spending almost all day on this OutOfMemory Error I was getting, turns out I simply wasnt advancing a cursor. However, I still wish to obtain an answer for my question below. To clarify, my question is:
Is it faster to make a custom adapter for a listview that hooks directly into the database to retrieve the data, or should an arraylist of that data be made first and then passed into a default arrayadapter?
I am currently working on an application and as a part of one of its functions I need to be able to take data from an internal database and display parts of it in various listviews. These listviews are all linked together in a ViewFlipper to make it easy to move between the views. I am working with about 5000 values maximum at once. (That is the stress size for the data set that I am tasked to work with).
Should I write a custom adapter that directly links to the database and extracts the values, or is there a better way to go about this? I tried to create a sort of wrapper class for the database that would extract all necessary data from the database and place it into a POJO but i keep getting OutOfMemory exceptions (5 string values * 5000 rows = 25000 strings doesnt seem to be nicely accepted in my case).
Not with the same amount of values, 5000 but I had a similar problem.
I ended up using a private arrayList on a ListAdapter, the list will contain only partial lists, for example 100 items.
Your cursor can initially contain the values to fill the firsts 100 items and when you scroll down looking for more items you can launch another cursor to retreive the next 50 items. Controlling a range of 100-150 items at your arrayList by adding/removing new/old items and refreshing the adapter.
I vote up your question because maybe someone find a better way to do it and I would like to know as well.

How to implement "Add to favorites" function in android listview

I load data from internet to build the listview, and put a star icon for each row. If user checked the star, I would save the entire row data into sqllite databse.
My question is that how can I know if one item has been added to favorites before to mark or not to mark the star checkbox. I know the database query is expensive and I can not do it in UI thread.
Just create an asynctask for each "getView" function to load the "favourites" data (if user fling too fast, there will be many asynctask pending) or do I have some other good options, or any 3rd lib for this?
At a high level, there's no reason you can't do a single query for favorites and pass that to a custom Adapter that also uses the internet data. Then in getView you can check to see if the current item is in the favorites.
Without seeing some code, it's hard to be more specific. Also, what's appropriate is going to depend on how many data items you are processing.
OK, i get my own answer, I get 10 items each time from internet, and then check the 10 items in database, assign "fav" value in all items, that works well.

Is it more efficient to load data to a listview from SQLite or create listviews from an array?

Hey guys I was just wondering about some efficiency issues. I am creating an app for android that has a few listviews, some listviews have 50+ items. Basically click one item on one listview will open up another listview that displays more options and so forth.
My question is right now I have these listviews populated from arrays but would it be more efficient to create a database and pull data to populate these listviews?
If this question has been asked already I apologize I have searched but have not found what I was looking for.
Beside of choosing between arrays or sql as datasource, is important to analyze the complexity of your view; sometimes is important "load views on demand" you may want consider this best-practices for optimization of your app: https://developer.android.com/training/improving-layouts/index.html and this https://developer.android.com/training/improving-layouts/smooth-scrolling.html
Best.-
If list data is not dynamic, I feel string arrays are best otherwise database is best option. database calls are always costly in terms of time/resources.
It depends on your data, if your data doesn't change , you can define them in xml file under ./value/. And sqlite just like other database, and it cost another I/O operations and it will reduce efficiency. But I think it doesn't cost too much in an android app.

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