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.
Related
Because ArrayLists are not particularly efficient, I would like to use a TreeSet in my Android App that will add items, and display them in alphanumeric order. Is there anything similar to an ArrayAdapter that I could use in order to more efficiently display the items within the TreeSet to the user?
Thanks for your help!
You can use whichever data structure you'd like by extending BaseAdapter. By extending BaseAdapter you can override the getView method and pass the correctly formatted view using your TreeSet as the data structure. This post shows a nice example of a BaseAdapter implementation, now they are using a List still but you can easily see how to implement the same thing using a TreeSet.
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.
I know there are several adapters to manipulate listviews like ArrayAdapter, BaseAdapter, CursorAdapter and so on, but I don't how to choose the best solution for my needs... I mean, when I'm choosing the adapter, which are the criterias that I should check before select the adapter to use?
Every kind of help will be appreciated!
The major concern in adapter selection is how you get your data to populate the list.
If the data is coming from a database/cursor, you should use one of the cursor adapters. You could use an array adapter, but that would just be unnecessary computational cycles to transfer the cursor data to the array before setting the adapter.
If your data is in an array then your obvious choice is one of the array adapter types.
If your data is coming from different sources, you'll need to create your own adapter or modify one of the existing ones.
After the incoming data format consideration might come thought on list modification and which adapter would work best with whatever needs you have to modify the list (and/or the data backing it).
Can anyone tell me what exactly does an array adapter do? I've tried searching the net but all I get is code examples. Please explain me what it does, I've visited the android developers as well.
An ArrayAdapter can be used as a data source for a number of different Android Views, such as a ListView or a Spinner.
Basically, you pass some kind of array or list to the constructor of an ArrayAdapter. Then, the adapter can be hooked up to a ListView by calling setAdapter(). You can also use the add and remove methods of the adapter to modify the underlying list itself.
You can also use an ArrayAdapter to customize the appearance of items in a ListView for example (or other Views) by using the constructor and passing in the resource of a layout to use, or by overriding the getView() method and building it yourself.
Typically, an adapter is some kind of translator. It's the "man in the middle" who know how to dialog with both sides and convert what is said.
An arrayAdapter is a class which get datas from an array and format it for a listview or spinner to understand it. When a listview need the data 4, for example, it will ask the adapter who will return him the 4 element of the array.
Ok, the listview could directly use the array.But with the adapter you're allowed to use any kind of data source. An ArrayAdapter(subclass of adapter) uses an Array, but another adapter could use a database or a file or anything else.That way the listview is able to get datas directly from any source without knowing how to access it.That is the adapter's role.
I am going to make a grid of images and I am trying to figure out whether to use an array adaptor or a baseadaptor. While the GridView example, stores the data in an array, it uses a BaseAdapter rather than a ArrayAdaptor. I am curious why this is. One thing I noticed about an ArrayAdapter, is that its constructor takes a textViewResourceId for some unknown reason - although the documentation say the getView can be used to make it work with other kinds of views as well. So, if I want a fixed grid of images for a menu, which class would you recommend choosing?
You typically choose your adapter class based on what the model data is. If you have an ArrayList of objects, use ArrayAdapter. If you have a Cursor from a database query, use a CursorAdapter. BaseAdapter can be used for anything, but it requires more coding, since it has no innate knowledge of how to iterate over the data.