I am doing a project according to the MVP pattern and faced the problem that the data is not displayed in the View. What am I doing wrong? What could be the problem? I understand that maybe something is wrong with the presenter.

BasePresenter class:

public abstract class BasePresenter<V> { protected V view; private CompositeSubscription compositeSubscription=new CompositeSubscription(); public void bindView(V view){ this.view=view; } public void unBindView(){ this.view=null; compositeSubscription.clear(); } protected void addSubscription(Subscription subscription){ compositeSubscription.add(subscription); } } 

PresenterFactory class:

 public class PresenterFactory { private final BaseApi baseApi; private static PresenterFactory presenterFactory; private PresenterFactory(BaseApi baseApi) { this.baseApi = baseApi; } public static void create(BaseApi baseApi){ if(presenterFactory ==null){ presenterFactory =new PresenterFactory(baseApi); } } public static PresenterFactory getInstance() { return presenterFactory; } public BasePresenter get(String tag) { if (tag.equals(ContactPresenter.TAG)) { return new ContactPresenter(baseApi); } return null; } } 

ContactPresenter class:

  public class ContactPresenter extends BasePresenter<ContactView> { public static final String TAG = ContactPresenter.class.getName(); BaseApi baseApi; private ArrayList<User> contacts; private ArrayList<Users> usersList; ContactAdapter contactAdapter; public ContactPresenter(BaseApi baseApi) { this.baseApi = baseApi; contacts=new ArrayList<>(); usersList=new ArrayList<> (); } public void onCreate(){ getUsers(); } private void getUsers() { Subscription subscription = BaseApi.getInstance().service.users() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(users -> { usersList.add(users); for (int i = 0; i < usersList.size(); i++) { contacts.addAll(usersList.get(i).getUser()); ); } }, throwable -> { Log.e("User errors", String.valueOf(throwable)); }); addSubscription(subscription); } } 

ContactView interface:

 public interface ContactView { void showData(ArrayList<User> contacts); } 

App class:

 public class App extends Application { @Override public void onCreate() { super.onCreate(); PresenterFactory.create(BaseApi.getInstance()); } } 

Fragment in which the presenter is used:

 public class ContactFragment extends Fragment implements ContactView { private ArrayList<User> contacts = new ArrayList<>(); private ArrayList<Users> usersList = new ArrayList<>(); private ContactAdapter contactAdapter; private RecyclerView rvView; private ContactPresenter mPresenter; private CompositeSubscription compositeSubscription = new CompositeSubscription(); public ContactFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_chat, container, false); rvView = (RecyclerView) view.findViewById(R.id.rvView); mPresenter = (ContactPresenter) PresenterFactory.getInstance().get(ContactPresenter.TAG); mPresenter.bindView(this); contactAdapter = new ContactAdapter(contacts, new OnItemClickListenerUser() { @Override public void onItemClick(User user) { } }); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); rvView.setLayoutManager(mLayoutManager); rvView.setAdapter(contactAdapter); contactAdapter.notifyDataSetChanged(); return view; } @Override public void onDestroy() { super.onDestroy(); mPresenter.unBindView(); } @Override public void showData(ArrayList<User> contacts) { this.contacts.addAll(contacts); contactAdapter.notifyDataSetChanged(); } } 

    1 answer 1

    Well, firstly, your presenter starts downloading data to his onCreate method, which nobody calls, and he will not call. Secondly, even if someone had called it, the data would still not be displayed, since Your Observer only adds data to the lists, but does not call the view method, which would display this data.

    Therefore, add view.showData(contacts) to the subscribe method, and call mPresenter.onCreate() for example in onCreaeView

    • I just tried, the showData () method works, but when I try to bring the ArrayList <User> contacts to showData () into the log, it turns out that this array is empty. - Lucky_girl
    • @Lucky_girl well, this is another question that has nothing to do with mvp. Look at your code one more time, maybe you will find the reason. - temq