title: Delete selected row in listview in android
body: <p>I have a listview with custom row having a textview and a clickable image for deleting the row. Listview gets the data from sqlite database. I have successfully loaded the data from the database into the listview. What I want is to delete that row from the database when delete button is pressed. I know this requires binding onclick event handler to the delete button and getting the object or sqlite data bound with the row. How can I get the sqlite data bound with the row on which the user clicked the delete button?
  Here is my adapter to the listview</p>
  
  <pre><code>public class CommentAdapter extends ArrayAdapter&lt;Comment&gt; {
  
  // private objects
  private List&lt;Comment&gt; mListComment;
  private LayoutInflater mInflater;
  
  public CommentAdapter(Context c,int textViewResourceId, List&lt;Comment&gt; list) {
      super(c, textViewResourceId, list);
      mListComment = list;
      // create layout inflater
      mInflater = LayoutInflater.from(c);
  }
  
  @Override
  public int getCount() {
      return mListComment.size();
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      // get view reference
      View view = convertView;
      // if null
      if(view == null) {
          // inflate new layout
          view = mInflater.inflate(R.layout.comment_row, null);
          // create a holder
          ViewHolder holder = new ViewHolder();
          // find controls
          holder.txtComment = (TextView)view.findViewById(R.id.txtComment);
          holder.btnDelete = (ImageView)view.findViewById(R.id.btnDelete);
          // set data structure to view
          view.setTag(holder);
      }
  
      Comment cmt = mListComment.get(position);
      // if not null
      if(cmt != null) {
          // query data structure
          ViewHolder holder = (ViewHolder)view.getTag();
          // set data to display
          holder.txtComment.setText(cmt.getComment());
          holder.btnDelete.setClickable(true);
          holder.btnDelete.setImageResource(android.R.drawable.ic_delete);
  
      }
  
      // return view
      return view;
  }
  
  /*
   * @class ViewHolder
   * to hold data structure on view with comment info
   */
  static class ViewHolder {
      private TextView txtComment;
      private ImageView btnDelete;
  
  }
  }
  </code></pre>
  
  <p>Here is my CommentsDataSource class</p>
  
  <pre><code>public class CommentsDataSource {
  private SQLiteDatabase database;
  private DatabaseHelper dbHelper;
  private String[] allColumns = { DatabaseHelper.COLUMN_ID, DatabaseHelper.COLUMN_COMMENT };
  
  public CommentsDataSource(Context context) {
      dbHelper = new DatabaseHelper(context);
  }
  
  public void open() throws SQLException {
      database = dbHelper.getWritableDatabase();
  }
  
  public void close() {
      dbHelper.close();
  }
  
  public Comment createComment(String comment) {
      //insert into databasae
      return newComment;
  }
  
  private Comment cursorToComment(Cursor cursor) {
      //convert cursor to comment
      return comment;
  }
  
  public void deleteComment(Comment comment) {
      //delete row from datbase
  }
  
  
  public List&lt;Comment&gt; getAllComments() {
      //get all rows
      return comments;
  }
  
  }
  </code></pre>