I am new to android and learning about the adapters. For this i am using the Android official documentation.
I am confused about one thing regarding adapters. Is the Adapter is super interface of all the adapters classes in android?
If yes then why this is not mentioned on this page that it is super interface of all the adapters in android. In fact it is on the top of hierarchy. See picture below:
and even this interface is also not in the hierarchy here: See picture below:
If no then how the methods of Adapter interface are implemented?
Or may be my understandings are wrong. Is this interface available to be implemented by custom user defined class or no it is implemented internally, or something else? Thank you very much in advance for the explanation and clarification of this confusion.
Is the Adapter is super interface of all the adapters classes in android?
No. It is for the classic AdapterView family of views (e.g., ListView, Spinner) and its set of adapters. RecyclerView.Adapter, PagerAdapter, etc. are unrelated to Adapter, other than they all fill the same sort of role.
and even this interface is also not in the hierarchy here: See picture below:
ArrayAdapter implements ThemedSpinnerAdapter, which extends Adapter. The JavaDocs do not point out the inheritance hierarchy of implemented interfaces.
Related
Currently for multi-ViewHolder implementations of RecyclerView everyone suggests to have a when/switch case in onCreateViewHolder()
e.g. here: How to create RecyclerView with multiple view type?
or here: https://medium.com/#gilbertchristopher/a-recyclerview-with-multiple-view-type-22619a5ad365
I'd like to have a cleaner onCreateViewHolder() method but need a way to move this decision making code into ViewHolders themselves.
Any ideas?
Here's my adapter class for more detail in github, I'm currently doing the decision making in baseViewHolder Class : https://github.com/davida5/DavidsResume/blob/master/app/src/main/java/davids/resume/screens/resume/ResumeAdapter.kt
I'm trying to add Pixate to our cross-platform app built with Xamarin.
In Android app I'm using custom ListView and custom adapter which implements an interface. But when the Adapter property of the list view is being set to my custom adapter its base class is Java.Lang.Object instead of BaseAdapter and does not implement the interface anymore (cast will cause an exception).
Anyone had a similar issue with Pixate ? How do I fix it ?
I've answered to this one on the github repo, so here is a copy-paste of that.
Pixate work on AdapterViews by proxying them in order to tag the recycled views with their new position (see https://github.com/Pixate/pixate-freestyle-android/blob/master/pixate-freestyle/src/com/pixate/freestyle/PXHierarchyListener.java#L232 ). This allows us to style more complex CSS expressions, like "nth-child" on a list.
Currently, that java.lang.reflect.Proxy is being created with a few known adapter interfaces, so the casting will fail if you would like to get your original BaseAdapter.
The good news is that we have a way to get to that original Adapter via PixateFreestyle.getAdapter(AdapterView view). Then you can cast to your BaseAdapter. That should not cause any problem.
I would like to know, from where we can know about what listener a View can implement. For Ex. there is no meaning in implementing onItemSelectedListener in custom Edit Text view class.
You should refer to the documentation (View, for example), there's a Nested Classes section that describes a list of interfaces that every View provides. For example, View class the OnClickListener interface, means every subclass of View would also provide it. And AdapterView (which is a parent of a ListView) provides the OnItemSelectedListener, means ListView will provide both OnClickListener and OnItemSelectedListener. Hope this helps.
You could take a look at the documentation, under the Inherited Methods.
Who knows about programming for iOS and data source/delegate paradigm will understand me.
The best practice for implementing custom view in iOS:
Implement MyView class inherited from UIView
MyView has dataSource field.
On drawRect: method MyView asks dataSource for items to draw.
dataSource object conforms to required protocol and implements needed methods.
It is very similar how is implemented UITableView, but I am not talking right now about cells.
Could you please let me know the best practices to implement custom view (like MyView) in Android with MVC pattern?
See MVC pattern on Android
Read all the answers to get a rounded view of android patterns as I don't find the first one very helpful.
Design decisions have been made regarding fundamental application components which prevent pure MVC being implemented, for example the use of activities which don't allow decoupling of the layers.
I find viewing android apps in a MVC way confuses matters as you end up misusing components like activities and adapters by trying to adapt them to perform a function they weren't really designed to do.
UPDATE
You need to make the question a little more specific to get a good answer, provide details of what sort of view you require (list, grid, simple) and what sort of models you are using (where are they persisted etc).
Although it's fairly subjective I have found the following when programming with Android:
Models tend to end up being unavoidably dumb (the anemic anti-pattern). This can be because in many occasions you will find the adapter is passed content to present in the view in the form of an object or collection and then operations on those objects or collections should be performed through the adapter so the adapter is aware of when they are changed and can manage the view accordingly.
The adapter can be treated as a link between model and view, a controller if you like but without many of the advantages of a pure MVC controller. I have found that customising the adapter can be effective (although you end up with 'fat controllers' if you are viewing it from an MVC perspective). Any UI input which would invoke changes to the content can then be called by adapter methods which will edit the models using add/remove for lists, or cursor operations for the database etc.
To implement a custom view in Android, you should derive from an existing View, then override the methods you need, which will often include onDraw(), onMeasure(), and probably one or more event handlers1.
To implement something in the spirit of MVC, the data that your component represents should not be in the view class itself but instead in other classes. The exact design is up to you: you can have data stored in files, in Java objects, pull it from a Content Provider2, etc.
When drawing (override onDraw()), you should look at the state of your data and draw accordingly;
When handling an event that alters the data, alter the data in the model and call invalidate() on the view to request that it be redraw to reflect the changes.
In my current application, which is for in-car use, I am displaying a series of Views (which represent gauges) at arbitrary locations within a RelativeLayout (the dashboard). There is obviously an amount of backing data that describes the positioning of those gauges, their attributes, the measurement data that they display, and so forth.
At this point, it would seem sensible for me to create a separate adapter class to contain the code that translates the backing data into the appropriate gauge Views. This would follow the existing Android pattern doing things, whereby various types of View (ListView, Spinner, etc.) accept an appropriate subclass of BaseAdapter to take care of interfacing to the backing data.
What I was considering doing is extending RelativeLayout so that it accepts an Adapter and calls the standard Adapter methods (like getCount(), getView(), and so forth). My custom Adapter would apply appropriate LayoutParams to each View so that it is appropriately positioned in the RelativeLayout.
So, rather than being a specific question as such, I'm just curious to know whether using the Adapter interface is suitable in this instance. What bugs me slightly is that in the Android library, Adapters seem to be commonly used for Views that present information in a list format. Do you think that subclassing Adapter (or BaseAdapter) sounds like a good pattern for what I'm doing, or is it going against the grain somewhat by using it in a situation where data isn't being presented in a list?
I'm just curious to know whether using the Adapter interface is suitable in this instance
IMHO, no, unless you have an arbitrary number of gauges. AdapterView is designed around supporting 0-N children using a recycling pattern to only require a few actual children. Adapter is designed around being used with AdapterView.