Am trying to implement chrisbane's pull-to-refresh library for my ListView (https://github.com/chrisbanes/Android-PullToRefresh). It seems simple enough but what Im having trouble with is the fact it seems to require it's own data set, seperate to what's managed by your list adapter, i.e.
mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));
All the examples I've seen just use this simple list of strings, but how do you go about using this with any kind of custom adapter, such as one that that contains a string and an imageview, etc?
There're plenty of examples on this site and around.
You create your adapter and set this arraylist as a field there
or usually set as a constructor parameter.
You #override getView() of this adapter and get your items using position parameter
in the getView(). Here look at the OrderAdapter class in this example:
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
and here at the GameAdapter class
http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx
hope this helps abit
Related
Few things in android can be done using directly XML Layouts then why do we use ArrayAdapter
Using ArrayAdapter is very short way to bind adapter in less code. If you are using ArrayList of Object, clearly ArrayAdapter is a better way than using BaseAdapter as -
BaseAdapter require a little more code.
Say for an example, Two lines of code and your adapter binding is done -
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
listview.setAdapter(itemsAdapter);
You are confused between differentiating a Layout and an Adapter. While a Layout is what you would inflate and display, Adapter is the one that would facilitate populating the the data for the displayed view. Adapter takes on the role of a mini-controller as in a MVC pattern.
Now, you want to work without an Adapter, you can! But if you have a list of data to display, you would ultimately use a List. An ArrayAdapter basically wraps this list as source of data and provides it to the view when asked for.
You can control how the data comes to the view by performing any pre-processing if you want. You can make the changes in the data set invisible to the View by managing them under the hood. All this is possible because of an Adapter and that's the Advantage of using an Adapter.
I'm having a difficult time creating a ListView using a custom class and a custom layout to visualise data. Creating a custom Adapter extending BaseAdapter doesn't seem to be capable of being updated with new data, and I haven't found any examples of using a custom ArrayAdapter that isn't based around simple data types. Any suggestions what should I do?
As long as you call notifyDataSetChanged() on BaseAdapter, there should be no problem updating your custom views with new data. I have used ListViews and RecyclerViews with custom views no problem using custom adapters.
I have a very simple question. I'm working on an Android application where I make use of Simple Adapter for ListView. Now I need to append items to this ListView dynamically. I know it is possible to do so in case of Array Adapters.
It is done as follows:
adapter.notifyDataSetChanged();
Is there any equivalent for the same in case of Simple Adapters? I couldn't find much relevant links regarding this on the net.
Kindly help.
Thanks in advance!
I think not, but you always can create a new SimpleAdapter and call setAdapter again with the new adapter.
If you are going to add/remove elements you, probably, need some other adapter not a SimpleAdapter. Because they say in the docs that SimpleAdapter is 'An easy adapter to map static data to views defined in an XML file'
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.