5

I have been implementing my android app with what I think is Passive MVP.

So for example in my view class I have a ListView.

View

ListView userListView;

and when an item is clicked, i simpley call a method on the presenter

userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mPresenter.onUserSelected(position);

    }

});

The part that I am confused about is that a ListView requires an adapter.

Presenter

So currently in my presenter I have this:

private ArrayList<User> mUserList = new ArrayList<User>();

...

adapter = new UserListAdapter(getContext(), mUserList);
mView.setUserListAdapter(adapter);

and when I want to change something I do this:

mUserList.add(user);
adapter.notifyDataSetChanged();

Is this the right place to the adapter? The reason I ask is because I was recently looking to do some work with swing, and a similar issue arises, JLists need a ListModel which seems pretty similar. So for swing, where would should the ListModel reside?

nPn
  • 16,254
  • 9
  • 35
  • 58
  • You may be looking for the [_adapter pattern_](http://en.wikipedia.org/wiki/Adapter_pattern), often required to match an existing component model to an application model. – trashgod Nov 04 '14 at 20:24
  • no, the question is more related to the MVP pattern. It seems like in mvp I should just be telling the view to update some a widget with new data, but since in this case the widget needs an adapter, I am not sure if the adapter belongs in the view or the presenter. I am not sure it really matters, but it's gnawing at my brain – nPn Nov 04 '14 at 20:32

2 Answers2

5

I believe you've correctly categorized your adapter as a Presenter.

The Presenter IS the adapter.

The adapter serves as the middle-man between the View (ListView) and Model (your List of Users) and provides View representations of each of the items in the List via the adapter's getView method.

Michael Krause
  • 4,689
  • 1
  • 21
  • 25
  • let me give it a day or so to see if I get any other thoughts, otherwise I will accept your answer. Kind of a freebie :) – nPn Nov 06 '14 at 01:05
  • I don't think so. As far as I know the adapter is also a view and it might have a presenter to work with as well. – Lennon Spirlandelli Mar 30 '17 at 00:10
0

In MVP pattern, the view layer should contains all platform dependencies. Check this answer.

To deal with the adapter abstraction you can use this.

Community
  • 1
  • 1
albertovecina
  • 93
  • 1
  • 4