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

  • Home
  • SEARCH
  • 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 721995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:56:16+00:00 2026-05-14T05:56:16+00:00

I apologize if this code looks a bit like a mess (considering the length);

  • 0

I apologize if this code looks a bit like a mess (considering the length); I figured I’d just include everything that goes on in my program at the moment.

I’m attempting to create a fairly simple Tic Tac Toe app for Android. I’ve set up my UI nicely so far so that there are a “grid” of TextViews. As a sort of “debug” right now, I have it so that when one clicks on a TextView, it should display the value of buttonId in a message box. Right now, it displays the correct assigned value for the first element I click, but no matter what I click afterwards, it always just displays the first value buttonID had. I attempted to debug it but couldn’t exactly find a point where it would pull the old value (to the best of my knowledge, it reassigned the value).

There’s a good possibility I’m missing something small, because this is my first Android project (of any note). Can someone help get different values of buttonId to appear or point out the error in my logic?

The code:

package com.TicTacToe.app;

import com.TicTacToe.app.R;
//Other import statements

public class TicTacToe extends Activity {
public String player = "X";
public int ALERT_ID;
public int buttonId;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Sets up instances of UI elements
    final TextView playerText = (TextView)findViewById(R.id.CurrentPlayerDisp);
    final Button button = (Button) findViewById(R.id.SetPlayer);
    final TextView location1 = (TextView)findViewById(R.id.location1);
    final TextView location2 = (TextView)findViewById(R.id.location2);
    final TextView location3 = (TextView)findViewById(R.id.location3);
    final TextView location4 = (TextView)findViewById(R.id.location4);
    final TextView location5 = (TextView)findViewById(R.id.location5);
    final TextView location6 = (TextView)findViewById(R.id.location6);
    final TextView location7 = (TextView)findViewById(R.id.location7);
    final TextView location8 = (TextView)findViewById(R.id.location8);
    final TextView location9 = (TextView)findViewById(R.id.location9);


    playerText.setText(player);

    //Handlers for events
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            if (player.equals("X")){
                player = "O";
                playerText.setText(player);
            }
            else if(player.equals("O")){
                player = "X";
                playerText.setText(player);
            }

            //Sets up the dialog
            buttonId = 0;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);
        }
    });

    location1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 1;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 2;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 3;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 4;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 5;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 6;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 7;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 8;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 9;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });
}

protected Dialog onCreateDialog(int id){
    String msgString = "You are on spot " + buttonId;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(msgString)
            .setCancelable(false)
            .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    }
                });
    AlertDialog alert = builder.create();
    return alert;
}

}
  • 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-14T05:56:16+00:00Added an answer on May 14, 2026 at 5:56 am

    You shouldn’t be calling onCreateDialog() yourself. Android does that for you when you call showDialog().

    I’m sure it only calls onCreateDialog() once and caches the value internally, which is why you’re only getting the correct value the first time, and then getting the same value for the rest.

    You’ll have to think of another way to do what you’re trying to do. Perhaps passing a different ID to showDialog() (be sure to handle that ID by creating the correct dialog in onCreateDialog()) is the way to go.

    Also, don’t import R manually.

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

Sidebar

Related Questions

So I currently have this code to read an accounts.txt file that looks like
i have an xml file that looks something like this <library> <book name=ABC> <chapter
I'm new to this site so I apologize if this question's formatting looks like
I apologize for this seemingly relatively simple task that I can't figure out. I
I apologize if this is a duplicate. I am writing a class that needs
I apologize if this is a duplicate question, I searched a bit and couldn't
I apologize if this is a duh question. It seems like the answer should
First of all I apologize in advance for this question, a bit off the
First off, if this is a duplicate, I apologize. I did quite a bit
I typically do not develop client-side (in this case, mobile) code, so my apologies

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.