2017-06-06 9 views
1

私は初めてのMVPアプリケーションを作成します。私が持っている問題はアダプタです。私は私のアダプターにはonclickメソッドを実装する私のrecyclerviewカード上のボタンがあり、今問題はアダプターです。なぜ私は私が私のアプリのプレゼンターの部分でそれを作ったビューのインターフェイスを実装するのか分からない。私はプレゼンターにビューを渡したいときにエラーが発生します。私のプレゼンターMVPパターンでアダプタを扱う方法

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 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) { 

    } 

} 

:私のフラグメントで :

これは私のエラー

enter image description here

これは私がやったすべてのものです

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 vonClickは、がFavoritePresenter.Viewと予想されている間に、android.view.Viewのタイプです。 、修正FavoritePresenter.Viewタイプに沿って通過するFavoritePresenterインスタンスを作成するには:

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

おかげで、それがうまく動作しますが、()関数を選択せず​​に、それはそこにすることになっていませんでした申し訳ありませんが – Sandro

+0

のためにその方法は何で、それを編集したたくさん – nbokmans

関連する問題