title: How to delete data from Sqlite on button click implemented in Recycler View in Android
body: <p>i need to delete item from database when we click on delete image button inside <code>recyclerView</code>.</p>
  
  <p>this is the basket activity
  BasketMain.java
  package in.haazir.haazir.basket;</p>
  
  <pre><code>            import android.app.ActionBar;
              import android.content.Intent;
              import android.support.v7.app.AppCompatActivity;
              import android.os.Bundle;
              import android.support.v7.widget.DefaultItemAnimator;
              import android.support.v7.widget.DividerItemDecoration;
              import android.support.v7.widget.LinearLayoutManager;
              import android.support.v7.widget.RecyclerView;
              import android.support.v7.widget.Toolbar;
              import android.util.Log;
              import android.view.Menu;
              import android.view.MenuItem;
              import android.view.View;
              import android.widget.ImageButton;
              import android.widget.ListView;
              import android.widget.Toast;
  
              import java.util.ArrayList;
              import java.util.List;
              import java.util.jar.Attributes;
  
              import in.haazir.haazir.R;
              import in.haazir.haazir.demo;
              import in.haazir.haazir.recycleviewlistner.RecyclerTouchListener;
              import in.haazir.haazir.sqllitedatabase.DatabaseOperations;
  
              public class BasketMain extends AppCompatActivity {
  
                  private RecyclerView haazirBasket;
                  DatabaseOperations basketobj;
                  List&lt;DataofBasket&gt; data=new ArrayList&lt;&gt;();
                  AdapterBasket mAdapter;
                  AdapterBasket mAdapter2;
                  private Toolbar mToolbar;
                  DataofBasket dob = new DataofBasket();
                  public int counter=0;
  
                  public String name;
                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.activity_basket_main);
                      //deletecartitem = (ImageButton)findViewById(R.id.basketdeleteitembtn);
                      this.setTitle("Basket");
                      this.getSupportActionBar().setDisplayHomeAsUpEnabled(true); // we use this when we use app compact else if we use actionbar then "this.getActionBar().setDisplayHomeAsUpEnabled(true);"
                      haazirBasket = (RecyclerView) findViewById(R.id.basketmain);
                      haazirBasket.setLayoutManager(new LinearLayoutManager(BasketMain.this));
                      haazirBasket.setItemAnimator(new DefaultItemAnimator());
                      //haazirService.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
                      haazirBasket.addItemDecoration(new in.haazir.haazir.recycleviewlistner.DividerItemDecoration(getApplicationContext()));
                      basketobj = new DatabaseOperations(this);
                      data = basketobj.getAllProducts();
                      mAdapter = new AdapterBasket(BasketMain.this,data);
  
                      haazirBasket.setAdapter(mAdapter);
  
  
  
                      haazirBasket.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), haazirBasket, new RecyclerTouchListener.ClickListener() {
                          @Override
                          public void onClick(View view, int position) {
                              Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();
                              // mAdapter.deleteListener(getApplicationContext());
  
                              DataofBasket dob = data.get(position);
                              name = dob.serviceName;
                              // mAdapter2 = new AdapterBasket(name,getApplicationContext());
                              /*if (counter &gt; 0) {
                                  float r = basketobj.DeletRecord(name);
                                  Log.e("deletItem: ", "" + r);
                                  data = basketobj.getAllProducts();
                                  mAdapter = new AdapterBasket(BasketMain.this, data);
  
                                  haazirBasket.setAdapter(mAdapter);
                              }*/
                          }
  
  
  
                          @Override
                          public void onLongClick(View view, int position) {
  
                          }
                      }));
  
  
  
                  }
                  String getName()
                  {
                      return name;
                  }
  
                  void updateCounter()
                  {
                      counter++;
                  }
                  void deletItem(int pos)
                  {
                              // mAdapter.deleteListener(getApplicationContext());
                      data = basketobj.getAllProducts();
                      DataofBasket dob = data.get(pos);
                      name = dob.serviceName;
  
                              float r =basketobj.DeletRecord(name);
                              Log.e("deletItem: ",""+r );
                              data = basketobj.getAllProducts();
                              mAdapter = new AdapterBasket(BasketMain.this, data);
  
                              haazirBasket.setAdapter(mAdapter);
  
                          }
  
  
  
  
                  @Override
                  public boolean onCreateOptionsMenu(Menu menu) {
                      // Inflate the menu; this adds items to the action bar if it is present.
                      getMenuInflater().inflate(R.menu.menu_main, menu);
                      return true;
                  }
  
                  @Override
                  public boolean onOptionsItemSelected(MenuItem item) {
                      // Handle action bar item clicks here. The action bar will
                      // automatically handle clicks on the Home/Up button, so long
                      // as you specify a parent activity in AndroidManifest.xml.
                      switch (item.getItemId()) {
                          case android.R.id.home:
                              // app icon in action bar clicked; goto parent activity.
                              this.finish();
                              return true;
                          case R.id.action_settings:
                              return true;
                          case R.id.cart:
                              return true;
                          default:
                              return super.onOptionsItemSelected(item);
  
                      }
  
                  }
  
  
  
              }
  </code></pre>
  
  <p>this is the <strong>Adapter</strong> i used for <code>recyclerView</code>.</p>
  
  <p>AdapterBasket.java
  package in.haazir.haazir.basket;</p>
  
  <pre><code>            /**
               * Created by Dell on 3/4/2017.
               */
              import android.content.Context;
              import android.support.v7.widget.RecyclerView;
              import android.util.Log;
              import android.view.LayoutInflater;
              import android.view.View;
              import android.view.ViewGroup;
              import android.widget.ImageView;
              import android.widget.TextView;
  
              import com.bumptech.glide.Glide;
  
              import java.util.Collections;
              import java.util.List;
  
              import in.haazir.haazir.R;
              import in.haazir.haazir.haazirservices.DataofServices;
              import android.content.Context;
              import android.support.v7.widget.RecyclerView;
              import android.view.LayoutInflater;
              import android.view.View;
              import android.view.ViewGroup;
              import android.widget.Button;
              import android.widget.ImageButton;
              import android.widget.ImageView;
              import android.widget.TextView;
              import android.widget.Toast;
              import android.widget.ToggleButton;
  
              import com.bumptech.glide.Glide;
  
              import java.util.Collections;
              import java.util.List;
  
              import in.haazir.haazir.R;
              import in.haazir.haazir.haazirservices.DataofServices;
              import in.haazir.haazir.sqllitedatabase.DatabaseOperations;
  
              /**
               * Created by hp.com on 10-02-2017.
               */
              public class AdapterBasket extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {
  
                  public Context context;
                  private LayoutInflater inflater;
                  List&lt;DataofBasket&gt; data= Collections.emptyList();
                  DataofBasket current;
                  DataofBasket current1;
                  int currentPos=0;
                  public int count = 1;
                  DatabaseOperations dbobj;
                  MyHolder holder;
                 public String name;
  
  
                  // create constructor to innitilize context and data sent from MainActivity
                  public AdapterBasket(Context context, List&lt;DataofBasket&gt; data){
                      this.context=context;
                      inflater= LayoutInflater.from(context);
                      this.data=data;
                  }
  
                  public AdapterBasket(String name, Context context){
                      this.name=name;
                      inflater= LayoutInflater.from(context);
  
                  }
                  // Inflate the layout when viewholder created
                  @Override
                  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                      View view=inflater.inflate(R.layout.container_basket, parent,false);
                      holder=new MyHolder(view);
                      return holder;
                  }
  
                  // Bind data
                  @Override
                  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  
                      // Get current position of item in recyclerview to bind data and assign values from list
                      final MyHolder myHolder = (MyHolder) holder;
                      current = data.get(position);
                      myHolder.textServiceName.setText(current.serviceName);
  
  
                      // load image into imageview using glide
                      Glide.with(context).load(current.serviceImage)
                              .placeholder(R.drawable.grocery)
                              .error(R.drawable.maggie)
                              .into(myHolder.serviceImageHaazir);
              /*myHolder.deletecartitem.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      Log.e( "onClick: ","button clicked "+current.serviceName );
                      DatabaseOperations basketobj = new DatabaseOperations(context);
                      BasketMain bm = new BasketMain();
                      float r =basketobj.DeletRecord(current.serviceName);
                      Log.e( "onClick: ",""+r );
                      //data = basketobj.getAllProducts();
                      //AdapterBasket ad = new AdapterBasket(context, data);
  
                      //ad.setAdapter(mAdapter);
                  }
              });
  
              */
                  }
                  // return total item from List
                  @Override
                  public int getItemCount() {
                      return data.size();
                  }
  
                  public void delete(int position) { //removes the row
                     current1 = data.get(position);
                      Log.e( "onClick: ","button clicked "+current1.serviceName );
                      BasketMain bm = new BasketMain();
                      //bm.deletItem(current1.serviceName);
                      //bm.updateCounter();
                      DatabaseOperations dbo = new DatabaseOperations(context);
                      float r = dbo.DeletRecord(name);
                      Log.e("deletItem: ", "" + r);
                      //data = dbo.getAllProducts();
  
                      //data.remove(position);
                      //notifyItemRemoved(position);
                  }
  
  
  
  
                  class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
  
                      TextView textServiceName;
                      ImageView serviceImageHaazir;
                      ImageButton deletecartitem;
  
                      // create constructor to get widget reference
                      public MyHolder(View itemView) {
                          super(itemView);
                          textServiceName = (TextView) itemView.findViewById(R.id.productdisplatnameidbasket);
                          serviceImageHaazir = (ImageView) itemView.findViewById(R.id.imagebasket);
  
                          deletecartitem = (ImageButton) itemView.findViewById(R.id.basketdeleteitmebutton);
                          deletecartitem.setOnClickListener(this);
                      }
  
                      @Override
                      public void onClick(View view) {
                          BasketMain bm = new BasketMain();
                          bm.deletItem(getAdapterPosition());
                          delete(getAdapterPosition());
                      }
                  }
  
  
  
  
              }
  </code></pre>