7

I'm trying to follow the MVP pattern. However, I have some doubts on how to handle adapters and view holders on this pattern.

Should I use the adapter as a presenter? Having the business logic?

Or should I pass a instance of the presenter that handles the list logic and then call methods of the presenter when there is any interaction with the adapter elements?

Thank you

Fábio Carballo
  • 3,245
  • 5
  • 26
  • 36
  • 1
    I think that Android view adapters are purely V-scope stuff, and if the adapters are presenters -- these presenters are internal somewhere in a view implementation. Also, presenters should not contain business logic as they are just mediators between M and V. In my current MVP-based project, presenters are not aware of list adapters at all as they should not care how a _list of something_ is displayed (e.g., a presenter justs tells a view: "display routes somehow", and just listens to a view event "onRouteSelected" as the route can be obtained from any kind of widgets, not necessarily a list). – Lyubomyr Shaydariv Dec 09 '15 at 14:00
  • 1
    So, do you have a reference to a presenter inside the adapter? Or do you add listeners to adapters events and then redirect to the presenter? @LyubomyrShaydariv – Fábio Carballo Dec 11 '15 at 10:46
  • Yes, the second case: the adapters can interact with the hosting views, and then a particual view decides how to transform/delegate an event to a presenter (or a presenter listener -- depends on how you separate presenters and listeners interfaces). – Lyubomyr Shaydariv Dec 11 '15 at 10:58

1 Answers1

4

There is no exact/correct definition of implementing MVP in Android

To answer your question, in my view the Presenter should not have any Android logic.

As such, the Adapter would be a "View" then i.e. Presenter provides it the data (via the Activity or Fragment) and it just deals with how to present this.

I'd do MVP as follow.

  1. Model - POJO's, parsing, Storing (SQLlite) and retrieving data (http). Obviously I'd divide the POJO's, parsing and DB logic into sub folders - but this all falls into Model for me.

  2. View - Activity, Fragment, Adapters - Activities & Fragment hold reference to a Presenter that gives them data to display. How this data/messages are displayed, look + feel etc. is dealt with in the View.

  3. Presenter - The Middle man, provides the logic to inputs i.e. Button Clicks, retrieval of data, validation of inputs & then passes the result back to the View (Activity or Fragment)

Here's a great article on MVP

Here's a simplified diagram of MVP

enter image description here

Answer modified from this question (also answered by me)

Community
  • 1
  • 1
Zain
  • 2,336
  • 1
  • 16
  • 25