gpt4 book ai didi

android - 如何在 MVP 模式中处理适配器

转载 作者:行者123 更新时间:2023-11-29 15:39:06 24 4
gpt4 key购买 nike

我将创建我的第一个真正的 MVP 应用程序。我遇到的问题是适配器。我的 recyclerview 卡上有一个按钮,我在适配器中实现了 onclick 方法,现在问题出在适配器上。我不知道为什么当我实现我在我的应用程序的演示者部分制作的 View 界面时。当我想将 View 传递给演示者时出现错误。这是我的错误

enter image description here

这就是我所做的一切:在我的 fragment 中:

public class FavoritFragment extends Fragment implements FavoritePresenter.View{
View rootView;


FavoritePresenter favoritePresenter;
public FavoritFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_address, container, false);
rootView=view;
favoritePresenter=new FavoritePresenter(this);

favoritePresenter.getAddressSearchModel();

return view;
}



@Override
public void updateFavoriteRecycler(ArrayList<AddressSearchModel> info) {

RecyclerView recyclerView=(RecyclerView)rootView.findViewById(R.id.address_recyclerview);
FavoriteAdapter adapterClass;
adapterClass=new FavoriteAdapter(info,getActivity(),rootView);
RecyclerView.LayoutManager LayoutManager= new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(LayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new
DividerItemDecoration(rootView.getContext(),LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(adapterClass);
adapterClass.notifyDataSetChanged();
}

@Override
public void deleteFavorite(ArrayList<AddressSearchModel> id) {

}

}

在我的演讲者中

public class FavoritePresenter {
// use this class to make networkCall Catch data and so on
View view;

ArrayList<AddressSearchModel> addressSearchModel;
public FavoritePresenter(View view){
this.view=view;
addressSearchModel=new ArrayList<>();
}

public void getAddressSearchModel()
{
addressSearchModel=PreparData();
view.updateFavoriteRecycler(this.addressSearchModel);
}
/**
* do the network call and delete favorite and then do another network call to update it.
* The better ways is to catch data and sort it base on update field and then send update field
* to ruby part and in ruby take the rest of field base on update_at field (better performance)
*/
public void deleteFavorite(AddressSearchModel favoriteId){
addressSearchModel=deleteData();
view.deleteFavorite(this.addressSearchModel);
}
private ArrayList<AddressSearchModel> deleteData() {
ArrayList<AddressSearchModel> lst_SearchResult=new ArrayList<>();;
AddressSearchModel model=new AddressSearchModel();
model.setName("sasd");
model.setAddress("sdsaqweqweqwds");
model.setRegions("asdfdgdfgqweqwedfasgfad");
model.setWorkingHours("2412312312");
model.setTelephone("22211111");

lst_SearchResult.add(model);
model=new AddressSearchModel();
model.setName("sas21312dqweqwe");
model.setAddress("sdsaderers");
model.setRegions("asdfdgdfgdfasgfad");
model.setWorkingHours("2412312312");
model.setTelephone("22211111");
lst_SearchResult.add(model);





return lst_SearchResult;
}
//call network call using this method to update recyclerview
private ArrayList<AddressSearchModel> PreparData() {
ArrayList<AddressSearchModel> lst_SearchResult=new ArrayList<>();;
AddressSearchModel model=new AddressSearchModel();
model.setName("sasd");
model.setAddress("sdsads");
model.setRegions("asdfdgdfgdfasgfad");
model.setWorkingHours("2412312312");
model.setTelephone("22211111");

lst_SearchResult.add(model);
model=new AddressSearchModel();
model.setName("sas21312d");
model.setAddress("sdsaderers");
model.setRegions("asdfdgdfgdfasgfad");
model.setWorkingHours("2412312312");
model.setTelephone("22211111");
lst_SearchResult.add(model);

model=new AddressSearchModel();
model.setName("saweresd");
model.setAddress("sdsererads");
model.setRegions("asdfdgererdfgdfasgfad");
model.setWorkingHours("2412312312");
model.setTelephone("22211111");
lst_SearchResult.add(model);


return lst_SearchResult;
}
public interface View
{
void updateFavoriteRecycler(ArrayList<AddressSearchModel> info);

//i get String as id because of UUID type format in ruby on rails
void deleteFavorite(ArrayList<AddressSearchModel> id);
}
}

在我的适配器中:

public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.MyHolder> implements FavoritePresenter.View {
private List<AddressSearchModel> doctorList;
public Activity activity;
public View rootView;
public FavoriteAdapter(List<AddressSearchModel> doctorList, Activity activity,View view)
{
rootView=view;
this.activity=activity;
this.doctorList=doctorList;
}




class MyHolder extends RecyclerView.ViewHolder{
public ImageView mArticleImage;
public TextView drugstore_desc;
public Button DeleteFavorite;
public MyHolder(View itemView) {
super(itemView);
mArticleImage=(ImageView)itemView.findViewById(R.id.im_article);
drugstore_desc=(TextView)itemView.findViewById(R.id.drugstore_desc);
DeleteFavorite=(Button)itemView.findViewById(R.id.DeleteFavorite);
}
}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview_address_montakhab,parent,false);

return new MyHolder(view);
}

@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
final AddressSearchModel addressSearchModel =doctorList.get(position);
Log.d("Doctore_Size", String.valueOf(doctorList.size()));
//holder.ImageView.setText(addressSearchModel.getName());
holder.drugstore_desc.setText(addressSearchModel.getRegions());

holder.DeleteFavorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fav_GetAddress(addressSearchModel.getName(), addressSearchModel.getRegions());
FavoritePresenter favoritePresenter=new FavoritePresenter(v);
favoritePresenter.deleteFavorite(addressSearchModel);
}
});
}

@Override
public void deleteFavorite(ArrayList<AddressSearchModel> id) {

}
@Override
public void updateFavoriteRecycler(ArrayList<AddressSearchModel> info) {
RecyclerView recyclerView=(RecyclerView)activity.findViewById(R.id.address_recyclerview);
FavoriteAdapter adapterClass;
adapterClass=new FavoriteAdapter(info,activity,rootView);
RecyclerView.LayoutManager LayoutManager= new LinearLayoutManager(activity);
recyclerView.setLayoutManager(LayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new
DividerItemDecoration(activity,LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(adapterClass);
adapterClass.notifyDataSetChanged();
}

public void fav_GetAddress(String place,String PlaceAddress){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog.setContentView(R.layout.dialog_add_address);
dialog.getWindow().setBackgroundDrawableResource(R.drawable.draw_radius_cost_info);
// set the custom dialog components - text, image and draw_button
EditText placeName=(EditText)dialog.findViewById(R.id.placeName);
EditText StreetAddress=(EditText)dialog.findViewById(R.id.StreetAddress);

placeName.setText(place);
StreetAddress.setText(PlaceAddress);

TextView dialogButton = (TextView) dialog.findViewById(R.id.dialogOK);
// if draw_button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
// custom dialog

}

@Override
public int getItemCount() {
return doctorList.size();
}
}

最佳答案

onClick 中的

View vandroid.view.View 类型,而您的 FavoritePresenter 期待一个FavoritePresenter.View。要解决此问题,请创建 FavoritePresenter 实例并传递 FavoritePresenter.View 类型:

    holder.DeleteFavorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fav_GetAddress(addressSearchModel.getName(), addressSearchModel.getRegions());
FavoritePresenter favoritePresenter=new FavoritePresenter(FavoriteAdapter.this);
favoritePresenter.deleteFavorite(addressSearchModel);
}
});

关于android - 如何在 MVP 模式中处理适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44384387/

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