0
我想將MVP模式添加到我的項目中。我爲視圖(ContactPresenter)創建了BasePresenter,PresenterFactory,主持人,但視圖中的數據不顯示。我做錯了什麼?或者可能是什麼問題?MVP - 視圖中的數據不顯示
這裏是BasePresenter類:
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:
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:
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);
}
}
接口ContactView:
public interface ContactView {
void showData(ArrayList<User> contacts);
}
類應用程序:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
PresenterFactory.create(BaseApi.getInstance());
}
}
片段,其中我想從演示顯示數據:
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();
}
}
我已經嘗試過了,但它不沒有幫助。我只是在showData()方法中添加了日誌,並且那裏的代碼根本不執行。 –
哦,這很奇怪。 PLZ嘗試修復第一個 – kevz
@Lucky_girl:U還沒有對showData()mth進行調用。 – kevz