使用 MVP 模式从数据库 onClick 中删除 ListView 项
Delete ListView item from database onClick, using MVP pattern
我正在我的应用程序中实现 MVP 模式,我需要在单击删除按钮时从 ListView 和数据库中删除一个项目,但我不想在适配器中实例化数据库,或者我'据我所知,我将打破 MVP 模式。
适配器class
public class ProductoAdapter extends ArrayAdapter<Product> {
private Context mContext;
private int layoutResourceId;
private List<Product> listaProductos;
public ProductoAdapter(@NonNull Context context, int resource, List<Product> list) {
super(context,0,list);
mContext = context;
layoutResourceId = resource;
listaProductos = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null) {
listItem = LayoutInflater.from(mContext).inflate(R.layout.producto,parent,false);
ButterKnife.bind(this,listItem);
}
final Product currentProducto = listaProductos.get(position);
TextView name = listItem.findViewById(R.id.nameTextView);
name.setText(currentProducto.getName());
TextView kilos = listItem.findViewById(R.id.kilosTextView);
kilos.setText(String.valueOf(currentProducto.getKilos()));
TextView price = listItem.findViewById(R.id.priceTextView);
price.setText(String.valueOf(currentProducto.getPricePerKilo()));
ImageButton delete = listItem.findViewById(R.id.delete_btn);
return listItem;
}
}
我想我应该使用 deleteButton.setOnClickListener(),但我如何使用 MVP 模式实现它?
您可以使用接口来实现。下面是实现
界面
public interface ProductoItemClickListener {
void onDeleteClick(Product product);
}
在适配器中
在适配器中定义侦听器变量class
private ProductoItemClickListener itemClickListener;
如下修改你的构造函数
public ProductoAdapter(@NonNull Context context, int resource, List<Product> list,ProductoItemClickListener itemClickListener ) {
super(context,0,list);
mContext = context;
layoutResourceId = resource;
listaProductos = list;
this.itemClickListener = itemClickListener
}
在 getView 方法中对 clicklistener 执行
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemClickListener.onDeleteClick(currentProducto );
});
在activity或片段
private ProductoItemClickListener mItemClickListener = new ProductoItemClickListener(){
@Override
public void onDeleteClick(Product product){
//perform action here
}
}
在初始化适配器时将此 mItemClickListener 作为最后一个参数传递。
这行得通!!
正如您指出的那样,在适配器中正确使用 deleteButton.setOnClickListener()
。这是布局的参考。
Interface - For Callback from Adapter - Delete Item Action → Activity
interface OnItemClickListener {
void onItemClick(int itemId)
}
Activity - Implements Interface → To handle callback from Adapter - Delete Item Action
Presenter - Handle the event from Activity to your Presenter
public class YourActivity extends AppCompatActivity implements
OnItemClickListener, YourView {
YourPresenter presenter;
YourAdapter adapter;
...
adapter.setListener(this);
....
@Override
public void onItemClick(final int itemId) {
// Your callback
presenter.delete(itemId)
}
}
Adapter - To register callback which will be sent to Activity
public class ProductoAdapter extends ArrayAdapter<Product> {
private final OnItemClickListener listener;
public setListener(final OnItemClickListener listener) {
this.listener = listener;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null) {
listItem = LayoutInflater.from(mContext).inflate(R.layout.producto,parent,false);
ButterKnife.bind(this,listItem);
}
final Product currentProducto = listaProductos.get(position);
...
ImageButton delete = listItem.findViewById(R.id.delete_btn);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(convertView, position, name);
}
});
return listItem;
}
}
我认为您可以使用组合实现您正在寻找的东西,您可以修改您的适配器(构造函数)以接收一个 class,它应该作为实现监听器的协调器工作。然后将其添加到您的删除按钮。
Class 应该处理在您的适配器上删除或调用方法的工作。
我正在我的应用程序中实现 MVP 模式,我需要在单击删除按钮时从 ListView 和数据库中删除一个项目,但我不想在适配器中实例化数据库,或者我'据我所知,我将打破 MVP 模式。
适配器class
public class ProductoAdapter extends ArrayAdapter<Product> {
private Context mContext;
private int layoutResourceId;
private List<Product> listaProductos;
public ProductoAdapter(@NonNull Context context, int resource, List<Product> list) {
super(context,0,list);
mContext = context;
layoutResourceId = resource;
listaProductos = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null) {
listItem = LayoutInflater.from(mContext).inflate(R.layout.producto,parent,false);
ButterKnife.bind(this,listItem);
}
final Product currentProducto = listaProductos.get(position);
TextView name = listItem.findViewById(R.id.nameTextView);
name.setText(currentProducto.getName());
TextView kilos = listItem.findViewById(R.id.kilosTextView);
kilos.setText(String.valueOf(currentProducto.getKilos()));
TextView price = listItem.findViewById(R.id.priceTextView);
price.setText(String.valueOf(currentProducto.getPricePerKilo()));
ImageButton delete = listItem.findViewById(R.id.delete_btn);
return listItem;
}
}
我想我应该使用 deleteButton.setOnClickListener(),但我如何使用 MVP 模式实现它?
您可以使用接口来实现。下面是实现
界面
public interface ProductoItemClickListener {
void onDeleteClick(Product product);
}
在适配器中
在适配器中定义侦听器变量class private ProductoItemClickListener itemClickListener;
如下修改你的构造函数
public ProductoAdapter(@NonNull Context context, int resource, List<Product> list,ProductoItemClickListener itemClickListener ) {
super(context,0,list);
mContext = context;
layoutResourceId = resource;
listaProductos = list;
this.itemClickListener = itemClickListener
}
在 getView 方法中对 clicklistener 执行
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemClickListener.onDeleteClick(currentProducto );
});
在activity或片段
private ProductoItemClickListener mItemClickListener = new ProductoItemClickListener(){
@Override
public void onDeleteClick(Product product){
//perform action here
}
}
在初始化适配器时将此 mItemClickListener 作为最后一个参数传递。
这行得通!!
正如您指出的那样,在适配器中正确使用 deleteButton.setOnClickListener()
。这是布局的参考。
Interface - For Callback from Adapter - Delete Item Action → Activity
interface OnItemClickListener {
void onItemClick(int itemId)
}
Activity - Implements Interface → To handle callback from Adapter - Delete Item Action
Presenter - Handle the event from Activity to your Presenter
public class YourActivity extends AppCompatActivity implements
OnItemClickListener, YourView {
YourPresenter presenter;
YourAdapter adapter;
...
adapter.setListener(this);
....
@Override
public void onItemClick(final int itemId) {
// Your callback
presenter.delete(itemId)
}
}
Adapter - To register callback which will be sent to Activity
public class ProductoAdapter extends ArrayAdapter<Product> {
private final OnItemClickListener listener;
public setListener(final OnItemClickListener listener) {
this.listener = listener;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null) {
listItem = LayoutInflater.from(mContext).inflate(R.layout.producto,parent,false);
ButterKnife.bind(this,listItem);
}
final Product currentProducto = listaProductos.get(position);
...
ImageButton delete = listItem.findViewById(R.id.delete_btn);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(convertView, position, name);
}
});
return listItem;
}
}
我认为您可以使用组合实现您正在寻找的东西,您可以修改您的适配器(构造函数)以接收一个 class,它应该作为实现监听器的协调器工作。然后将其添加到您的删除按钮。
Class 应该处理在您的适配器上删除或调用方法的工作。