Clicking Elements in Android Doesn't Display the Correct Values

Posted by Devin on Stack Overflow See other posts from Stack Overflow or by Devin
Published on 2010-04-14T02:03:59Z Indexed on 2010/04/14 3:33 UTC
Read the original article Hit count: 175

Filed under:
|
|

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;
}

}

© Stack Overflow or respective owner

Related posts about java

Related posts about android