gpt4 book ai didi

java - MVP - View 中的数据不显示

转载 作者:行者123 更新时间:2023-12-01 09:14:00 25 4
gpt4 key购买 nike

我想将 MVP 模式添加到我的项目中。我已经为 View (ContactPresenter)制作了BasePresenter、PresenterFactory、presenter,但是 View 中的数据不显示。我做错了什么?或者可能是什么问题?

这是 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);
}
}

接口(interface)ContactView:

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

类应用程序:

public class App extends Application {

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

我想在其中显示来自演示者的数据的 fragment :

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(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();

}

关于java - MVP - View 中的数据不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40713257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com