Android 如何在MVP模式中处理适配器
我将创建我的第一个真正的MVP应用程序。我遇到的问题是适配器。我的recyclerview卡上有一个按钮,我在adapter中实现了onclick方法,现在问题出在adapter上。我不知道为什么我在应用程序的presenter部分实现了视图界面。我想将视图传递给演示者时出错。 这是我的错误 这就是我所做的一切: 在我的片段中:Android 如何在MVP模式中处理适配器,android,android-recyclerview,mvp,Android,Android Recyclerview,Mvp,我将创建我的第一个真正的MVP应用程序。我遇到的问题是适配器。我的recyclerview卡上有一个按钮,我在adapter中实现了onclick方法,现在问题出在adapter上。我不知道为什么我在应用程序的presenter部分实现了视图界面。我想将视图传递给演示者时出错。 这是我的错误 这就是我所做的一切: 在我的片段中: public class FavoritFragment extends Fragment implements FavoritePresenter.View{
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) {
}
}
公共类FavoritFragment扩展片段实现FavoritePresenter.View{
视图根视图;
推荐人推荐人推荐人;
公共偏好片段(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u地址,容器,false);
rootView=视图;
favoritePresenter=新的favoritePresenter(此);
favoritePresenter.getAddressSearchModel();
返回视图;
}
@凌驾
public void updateFavoriteRecycler(ArrayList信息){
RecyclerView RecyclerView=(RecyclerView)rootView.findViewById(R.id.address\u RecyclerView);
FavoriteAdapterClass;
adapterClass=新的FavoriteAdapter(信息,getActivity(),rootView);
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(getContext());
recyclerView.setLayoutManager(LayoutManager);
setItemAnimator(新的DefaultItemAnimator());
回收视图。附加装饰(新)
DividerItemDecoration(rootView.getContext(),LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(适配器类);
adapterClass.notifyDataSetChanged();
}
@凌驾
公共void deleteFavorite(ArrayList 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);
}
}
公共类收藏夹演示者{
//使用此类生成networkCall捕获数据等
视图;
ArrayList地址搜索模型;
公共收藏夹演示者(视图){
这个视图=视图;
addressSearchModel=新的ArrayList();
}
public void getAddressSearchModel()
{
addressSearchModel=PreparData();
view.updateFavoriteRecycler(this.addressSearchModel);
}
/**
*进行网络呼叫并删除收藏夹,然后进行另一次网络呼叫以更新收藏夹。
*更好的方法是捕获数据并根据更新字段对其进行排序,然后发送更新字段
*到ruby部分,在ruby中,在字段更新_的基础上获取字段的其余部分(更好的性能)
*/
public void deleteFavorite(AddressSearchModel favoriteId){
addressSearchModel=deleteData();
view.deleteFavorite(this.addressSearchModel);
}
私有ArrayList deleteData(){
ArrayList lst_SearchResult=新建ArrayList();;
AddressSearchModel=新的AddressSearchModel();
model.setName(“sasd”);
model.setAddress(“sdsaqweqwds”);
model.setRegions(“asdfdgdfgqweqwedfasgfad”);
型号:设定工作时间(“2412312”);
型号:Settelphone(“22211111”);
lst_SearchResult.add(模型);
model=新地址searchmodel();
model.setName(“sas21312dqweqwe”);
model.setAddress(“sdsaderers”);
model.setRegions(“asdfdgdfasgfad”);
型号:设定工作时间(“2412312”);
型号:Settelphone(“22211111”);
lst_SearchResult.add(模型);
返回lst_搜索结果;
}
//使用此方法调用网络调用以更新recyclerview
私有ArrayList PreparData(){
ArrayList lst_SearchResult=新建ArrayList();;
AddressSearchModel=新的AddressSearchModel();
model.setName(“sasd”);
model.setAddress(“sdsads”);
model.setRegions(“asdfdgdfasgfad”);
型号:设定工作时间(“2412312”);
型号:Settelphone(“22211111”);
lst_SearchResult.add(模型);
model=新地址searchmodel();
model.setName(“sas21312d”);
model.setAddress(“sdsaderers”);
model.setRegions(“asdfdgdfasgfad”);
型号:设定工作时间(“2412312”);
型号:Settelphone(“22211111”);
lst_SearchResult.add(模型);
model=新地址searchmodel();
model.setName(“saweresd”);
model.setAddress(“sdsererads”);
model.setRegions(“asdfdgeredfgdfasgfad”);
型号:设定工作时间(“2412312”);
型号:Settelphone(“22211111”);
lst_SearchResult.add(模型);
返回lst_搜索结果;
}
公共界面视图
{
void updateFavoriteRecycler(ArrayList信息);
//由于RubyonRails中的UUID类型格式,我将字符串作为id
void deleteFavorite(ArrayList 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();
}
}
公共类FavoriteAdapter扩展了RecyclerView。适配器实现了FavoritePresenter.View{
私人名单医生名单;
公共活动;
公众观点;
公共收藏夹适配器(列表文档列表、活动、视图)
{
rootView=视图;
这个。活动=活动;
this.doctorList=doctorList;
}
类MyHolder扩展了RecyclerView.ViewHolder{
公众形象;公众形象;
公共文本视图药店描述;
公共按钮;
公共MyHolder(查看项目视图){
超级(项目视图);
mArticleImage=(ImageView)itemView.findViewById(R.id.im_文章);
drugstore_desc=(TextView)itemView.findViewById(R.id.drugstore_desc);
DeleteFavorite=(按钮)itemView.findViewById(R.id.DeleteFavorite);
}
}
@凌驾
public MyHolder onCreateViewHolder(视图组父级,int-viewType){
View=LayoutInflater.from(parent.getContext())
.充气(R.布局.卡片视图\地址\蒙塔卡布,家长,假);
回来
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);
}
});