Lucky_girl Lucky_girl - 18 days ago 4
Java Question

MVP - data in the view doesn't display

I want to add MVP pattern into the my project. I have made BasePresenter, PresenterFactory, presenter for the view (ContactPresenter), but data in the view doesn't display. What I'm doing wrong? or what can be the problem?

Here is 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);
}
}


Class PresenterFactory:

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;
}
}


Class ContactPresenter:

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());
);
}
view.showData(contacts);
}, throwable -> {
Log.e("User errors", String.valueOf(throwable));
});

addSubscription(subscription);
}
}


Interface ContactView:

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


Class App:

public class App extends Application {

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


Fragment in which I want to display data from presenter:

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) {

}
});



// showData(contacts);
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=contacts;
contactAdapter.notifyDataSetChanged();

}
}

Answer

Make below in method showData(ArrayList<User> contacts)

@Override
    public void showData(ArrayList<User> contacts) {
        this.contacts.addAll(contacts);
        //contactAdapter = new ContactAdapter(contacts, new OnItemClickListenerUser() {
        //    @Override
        //      public void onItemClick(User user) {

        //    }
        //});
        contactAdapter.notifyDataSetChanged();

    }