Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does an "adapter" fit in MVP (passive view)?

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?

like image 795
nPn Avatar asked Nov 04 '14 20:11

nPn


1 Answers

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.

like image 64
Michael Krause Avatar answered Oct 11 '22 16:10

Michael Krause