我要创建我的第一个真正的MVP应用程序。我有的问题是适配器。我在我的recyclerview卡上有一个按钮,我在适配器中实现onclick方法,现在问题在于适配器。我不知道为什么当我实现我在我的应用程序的演示者部分中创建的视图界面时。当我想将视图传递给演示者时,我收到错误。 这是我的错误
这就是我所做的一切: 在我的片段中:
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();
}
}
答案 0 :(得分:4)
View v
内的{p> onClick
类型为android.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);
}
});