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 8821699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:55:19+00:00 2026-06-14T05:55:19+00:00

My ListViewItems have delete buttons in them. From those button’s click events I want

  • 0

My ListViewItems have delete buttons in them. From those button’s click events I want to show a confirmation dialog before deleting the item via it’s ID from the database. The ID is stored in the item’s ViewHolder.

How can I access the item’s ViewHolder from the AlertDialog’s click handler? Here is the relevant code. Compiler chokes on “V” inside onClick(DialogInterface dialog, int whichButton).

I could store the ID in the button’s tag but that feels awkward.

I’m targetting minimum API level 8 but let me know if higher API levels have a solution for this. It’s my first Android program so there may very well be an obvious solution.

private static class MyAdapter extends CursorAdapter {
//.....
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = View.inflate(context, R.layout.my_detail, null);
        MyViewHolder holder = new MyViewHolder();
        holder.deleteButton = (Button) view.findViewById(R.id.deleteButton);
        holder.deleteButton.setOnClickListener(deleteButtonClickListener);
        holder.editButton = (Button) view.findViewById(R.id.editButton);
        holder.editButton.setOnClickListener(editButtonClickListener);
        holder.nameTextView = (TextView) view
                .findViewById(R.id.nameTextView);
        holder.itemId = cursor.getLong(cursor
                .getColumnIndex(MyData.ID_COLUMN));
        view.setTag(holder);
        return view;
    } // newView()
//.....
    private OnClickListener deleteButtonClickListener = new OnClickListener() {

        public void onClick(View v) {
            new AlertDialog.Builder(_context)
                    .setTitle("Delete?")
                    .setMessage("Delete item?")
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setPositiveButton(android.R.string.yes,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    MyViewHolder holder = (MyViewHolder) ((View) v
                                            .getParent()).getTag();
                                    long itemId = holder.itemId;
                                    _MyData.deleteItem(itemId);
                                }
                            }).setNegativeButton(android.R.string.no, null)
                    .show();
        } // onClick()

    }; // deleteButtonClickListener
//.....
}
  • 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-14T05:55:20+00:00Added an answer on June 14, 2026 at 5:55 am

    Your code actually looks pretty good, the only change needed is that v needs to be declared final, like so:

    private OnClickListener deleteButtonClickListener = new OnClickListener()
    {
        public void onClick(final View v) { }
    }
    

    The reason for this is due to how Java implements closures. v should be final so that our implementation of DialogInterface.OnClickListener in the setPositiveButton() has access to the variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView full of ListViewItems. I want to emphasize some of them
I have a button inside a listview to delete selected item.when i click on
I have a Listview with some data.I want to get the number of click
I have a listactivity which contains a listview. i want to show another layout
I have several ListViewItems i want to order it in different way using its
I want to remove whole ListviewItems in my Listview except first Column. I have
I have an html button and I'd like to have a server side click
I have a ListView. I want to select the first element from it and
I have a class that desccends from ListviewItem. When I create an instance of
I'm copying ListViewItems from one ListView to another, sth. like: foreach (ListViewItem item in

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.