Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:24:38+00:00 2026-06-01T06:24:38+00:00

I have a listview with custom row having a textview and a clickable image

  • 0

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

public class CommentAdapter extends ArrayAdapter<Comment> {

// private objects
private List<Comment> mListComment;
private LayoutInflater mInflater;

public CommentAdapter(Context c,int textViewResourceId, List<Comment> 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;

}
}

Here is my CommentsDataSource class

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<Comment> getAllComments() {
    //get all rows
    return comments;
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:24:39+00:00Added an answer on June 1, 2026 at 6:24 am

    Mobify your getView method as below:

    @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);
        }
    
        final 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);
            holder.btnDelete.setOnClickListener(new View.OnClickListener(){
                   public void onClick(View view)
                   {
                         commentsDataSource.deleteComment(cmt);
                         //Requery DB to get Updated results
                         notifyDataSetChanged();
                   }
            })
    
        }
    
        // return view
        return view;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom listview which display an image, textview and radio button. I
I am needing help populating data from a database into a custom ListView. I
I have a custom listview row that contains a number of textView components. Instead
I have a ListView with some custom layout for the row (an image, then
I have got ListView with a custom Adapter. Every row contains clickable buttons and
I have a listview with custom rows and that extends SimpleAdapter. Each row consist
I am populating the ListView from my Custom DataAdaptor. I have a layout defined
I have a listview that's populated by rows that get their data from a
I have a a listview with each row having a text field and edittext
I have a ListView which contains custom rows. This custom row has following UI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.