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.
Related
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.
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.
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.
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.
Hello
I am trying to build a listview based on content from the web.
I have had a look at :
http://androidboss.com/load-listview-in-background-asynctask/
example but it uses a predefined array of months. How can I substitute the known
array of months for an unknown undetermined number of items from the internet?
I want to load a listview with some data from the internet, the user
scrolls the list and it retrieves the next row(s) from the internet etc etc
rather than using a array of predetermined length.
Thanks Ian
You can use my EndlessAdapter for that. The project has a demo/ subproject demonstrating its use.
It sounds like you need to extend an adapter such as ArrayAdapter. Extending an ArrayAdapter so that you can dynamically generate the rows or alter the number of rows, and also notify the Adapter that the underlying data has changed, is a very common exercise in Android.
You'll find quite a few tutorials on this but, very basically, if you implement your own adapter by extending ArrayAdapter you can override getView() to programmatically generate each view, and you can override getCount() to provide the number of rows. You can use notifyDataSetChanged() to trigger a refresh of the list on the screen if some data has changed and you need to refresh.