I have relatively small H2 database. I am expecting no more than 100 entries and even that feel highly over the top.
I have a list view with some controls and use an ArrayAdapter with a call to QueryAll() to populate the data. It works perfectly as I can modify the data in the array in memory when a button is pressed and then write the result to DB without having to reload it. But the initial load is surprisingly slow
What I wonder is whether I should rather use a CursorAdapter, since it seems more fitting to the problem or write a custom adapter to make use of the DAO Iterator.
Will there be a performance improvement using Cursor or a custom adapter? In my mind it feels like a custom adapter should give the best performance.
CursorAdapter is more appropriate when there is a database because it does not load all the records as ArrayAdapter. It loads only the visible records in the ListView is between 5 and 10.
So I think if you use a CursorAdapter you will not have loading problems.
Regards
Its better to use a custom adapter with
Content provider.
Related
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.
I need to know which adapter is the best and fast performance between CursorAdapter and BaseAdapter? I always use BaseAdapter when I'm trying to display in ListView. But I know CursorAdapter is updated one and easy to manipulate but not sure which one is better. Or which adapter (or any adapter else) should I use to retrieve data from sqlite and display at ListView?
As seen in the CursorAdapter source, CursorAdapter is just a thin, convenience wrapper around BaseAdapter as you would expect and performs no expensive operations. The vast majority of the time spent displaying items will be in inflating, measuring, and laying out your views which needs to be done no matter what adapter you use. Therefore you should always use the one that provides the closest implementation to your needs. For SQLite database, that is almost exclusively CursorAdapters.
Should I use array adapter or base adapter or cursor adapter?
What do you use most? I found some code that uses base adapter for fragments. Can I use array adapter or cursoradapter for listfragments?
I know how to use listview in a simple way like using the android.r.simple. I want to know what I should use in creating a listview that uses listfragment and populating it with data that came from SQLite.
What is the easiest adapter to use to achieve this?
There are difference between the different adapter class. You should decide to use one depending on your model data.
ArrayAdapter is better if you have an ArrayList of objects.
CursorAdapter is better if you have a database query and a Cursor
BaseAdapter is the most customizable, so you can use it for anything (you have to customize it a little bit more than others)
I normally end up extending BaseAdapter. It's simple enough, and ArrayAdapter isn't flexible enough to handle the case where items have multiple values that need to go into different fields in your list item.
Good day, this might be a silly question, am having a bit of dilemma as to how to create a listview with an imageview and two textview. I am using a cursor to query the MediaStore, but i don't know which adapter to use. I would have thought using Cursor Adapter, but keep seeing numerous examples of using an ArrayAdapter. so am asking, which adapter will best be used to create the listview? is there anything wrong in using CursorAdapter for this my problem or is CursorAdapter only used when you are getting data from a Sql database. Thank you.
Either should work for you, however the CursorAdapter is ... mainly for working with Cursors. As you Query the MediaStore you will notice that you are working with a Cursor, so this would be the best route.
ArrayAdapter will also work, but this is mainly when you are working with Lists or Arrays of items (List of MyObject or MyObject[]).
This question has an example of using a CursorAdapter with MediaStore. Here is a good read on Content Providers (which you might have already read).
I need to implement a ListAdapter for multiple row types. So pick a layout per row based on the content of that row. I would like to hear some opinions on the merits of the different types of ListAdapters for accomplishing this. I don't expect to have thousands of rows or even a hundred, but I may potentially have quite a few different layouts and need a flexible easy to understand implementation.
So I am considering
BaseAdapter
ArrayAdapter
CursorAdapter
SimpleCursorAdapter
What are some of the advantages/disadvantages of these adapters. Would a cursor based adapter be better?
I want a it to be flexible, easy to change and work reliably.
The adapters are there to adapt a data source to the view, so it really comes down to your data source.
If its a database data source and you have largely straight-froward mappings of columns to text and image views, start with SimpleCursorAdapter.
If its a database data source but a lot of custom mappings or if you end up overriding a lot of SimpleCursorAdapter's functionality anyway, take a look at CursorAdapter.
If the data source is something that can viewed as an array, the look at ArrayAdapter.
If its a custom data source or if there's a alot of custom logic for binding the data to the views that doesn't fit the other adapters, derive your own adapter from BaseAdapter.