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.
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 trying to make a android app with a scrollable list like this one...
When I made this app in iOS, I used a UICollectionView. What`s the best way to make this in android studios?
I tried using a list view, but I can't seem to customize it to my needs?
Any Ideas?
ListView was a great way to start and it is customizable to your needs.
However I would recommend to use RecyclerView which works almost on the same principle as ListView but it is a newer concept in Android. It handles the usage of the ViewHolder pattern for you which makes everything super easy.(With ListView, you would've had to implement your own ViewHolder pattern)
All you need to do is to have the RecyclerView in your activity/fragment as the view to hold your data. Then, the key component is to implement the RecyclerView's Adapter which will handle the inflation and setup of each list item.
Here is a really great and short tutorial to get you started with RecyclerView.
If you're done with that here is a bit more advanced video material on RecyclerView from Dave Smith explaining a lot of ways on how to understand and use RecyclerView in your apps.
A ListView fed by an ArrayAdapter of custom objects can do the job.
You might have a class that contains the text and the image of a single item.
Then you can create an ArrayList with items of that class, where you put in all the data.
This ArrayList can be used as dataset for a custom ArrayAdapter, which overrides the getView()-method.
The getView()-method must inflate the xml-layout for each item and attach the data from the ArrayList to the inflated view.
You might want to implement the Viewholder-pattern to achieve a good performance and avoid unnecessary inflations and getViewByID()-calls.
You can also use the new CardView, which can make it a lot easier, since it is made for the new material design in android 5, which looks similar to your item list.
I am trying to implement a grouped listview on Android similar to iOS. Therefore, I am trying to write my own custom MvxAdapter that supports grouped section headers. The default MvxListView constructed from axml will create a default MvxAdapter. Since I need to supply my own custom MvxAdapter, I need to create the MvxListview programmatically so I can pass in my own adapter. The problem I am having is at the time of OnCreate of my android view where I try to construct my custom MvxAdapter, the Android binding context is null as retrieved from
MvxAndroidBindingContextHelpers.Current()
Is there an example of constructing an MvxListView programmatically with a custom MvxAdapter with v3 API?
There's no examples of creating an MvxListView programatically - almost all Android UI controls are created in axml in the current samples.
For creating custom adapters, there are a few examples around, inclduing:
an example in the polymorphic list in the collection at: https://github.com/slodge/MvvmCross-Tutorials/tree/master/Working%20With%20Collections
an advanced example in the https://github.com/slodge/MvvmCross-Tutorials/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Droid/Views/SessionsLists/BaseSessionListView.cs
Alternatively, you can, of course, inherit a CustomListView from MvxListView and can then pass in your custom adapter as part of the constructor.
For more on creating and using custom views, see http://slodge.blogspot.co.uk/2013/05/n18-android-custom-controls-n1-days-of.html
In the event that you ever do want to push a context onto the stack you can do this using:
using (new MvxBindingContextStackRegistration<IMvxAndroidBindingContext>(**TheContext**))
{
// create your controls here
}
This is exactly what happens during xaml inflation - see: https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding.Droid/BindingContext/MvxAndroidBindingContext.cs#L47
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
I'm new to Android development and I was coding with ListView with a custom adapter. So far so good, then I wanted to use the same adapter with more than one listview, is that ok?
And when I override the method getView(), then I use the same resource to show the views (eg. R.id.show_view). Can I use different layouts in the same adapter? I don't know how to accomplish that.
I dont have the code here, sorry, it's more a question of whether it's a good practice to use the same adapter (eg. ArrayAdapter) to match various ListViews.
You can reuse class, but no instance of adapter. And you can create different views for every entry. But for sake of performance, you shall reuse view supplied to getView() whenever possible