2017-06-06 62 views
1

我要創建我的第一個真正的MVP應用程序。我的問題是與適配器。我在我的recyclerview卡上有一個按鈕,我在適配器中實現了onclick方法,現在問題出在適配器上。我不知道爲什麼當我實現視圖界面時,我在應用程序的演示者部分製作了它。我想要將視圖傳遞給演示者時出現錯誤。 這是我的錯誤如何在MVP模式下處理適配器

enter image description here

這是什麼,我所做的一切: 在我的片段:

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

回答

3

View v裏面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); 
     } 
    }); 
+0

非常感謝它運作良好,但沒有select()函數,什麼是該方法 – Sandro

+0

比較遺憾的是不應該在那裏,編輯出來 – nbokmans