androidandroid-mvp

Delete ListView item from database onClick, using MVP pattern


I'm implementing MVP pattern in my app, and I need to delete an item from the ListView and Database when clicked a delete button, but I don't want to instantiate the DB in the Adapter, or I'll be breaking MVP pattern as I understand.

Adapter 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;
    }
}

I guess I should use deleteButton.setOnClickListener(), but how I implement this with MVP pattern?


Solution

  • As you pointed it out right use deleteButton.setOnClickListener() in adapter. Here's a reference to layout this.

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