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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:52:05+00:00 2026-05-23T23:52:05+00:00

I have a list of items, created by a listview. I would like to

  • 0

I have a list of items, created by a listview. I would like to long press one of the items on the list and an alert dialog to open up and depending on yes or no key on that dialog box I wan to set a global variable. The code that I am using is inside “MyActivity.java” and looks like this:

ListView lv = getListView();
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> av, View v, int pos, final long id) {

        final AlertDialog.Builder b = new AlertDialog.Builder(MyActivity.this);
        b.setIcon(android.R.drawable.ic_dialog_alert);
        b.setMessage("Are you sure?");
        b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    yesOrNo = 1;
                }
        });
        b.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    yesOrNo = 0;
                }
        });

        b.show();

        if (yesOrNo == 1) {
            DO SOMETHING;   
        }
        return true;
    }
});

However, the global variable “yesOrNo” is not changing no matter if I press “Yes” or “No”.
Can somebody let me know what is wrong with the code?

Thanks for the help.

  • 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-05-23T23:52:05+00:00Added an answer on May 23, 2026 at 11:52 pm

    AlertDialog does not wait for the selection. After you call show() method, these two lines will be executed immediately:

    if (yesOrNo == 1) {
            DO SOMETHING;   
    }
    

    So value of yesOrNo variable will be its initial value.

    Solution:

    You can call doSomething(0) in onClick() method of positiveButton and doSomething(1) in onClick() method of negativeButton.

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

Sidebar

Related Questions

I would like to create a listview with custom rows. I have created a
I have created a android list view in Eclipse that I would like to
I have created a list view that displays the names and dates of items
I have a list of items that are created from the results of website
I have a simple unordered list with list items as menu item i created
I have created a TableView(Grouped Table View). And I can list the items in
I have a dynamically created list. Items from the list should be able to
Within my code I have list items like this; <div id=content> <ul class=kwicks> <li
I would like to create ListView using CustomAdapter that extends BaseAdapter . I have
In my android application I would like to display items in a list. For

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.