更新一个循环中的onClickListener按钮中的数组元素 - android

时间:2012-02-01 21:45:05

标签: java android arrays loops

基本上,我正在为android实现一个reversi应用程序,我目前正在尝试更新二维数组中的一个元素。该调用位于onClickListener中,该onClickListener位于用于设置反转板的循环中。问题是,一旦放置了一块,元素isPositionEmpty应该变为false,但是,它不会。以下是代码片段:

for(int n = 0; n < 8; n ++){

        ...


    for(int i = 0; i < 8; i++ ){
        final ImageView button = new ImageView(this);

        final int countN = n;
        final int countI = i;

                    ...

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String buttonID = String.valueOf(button.getId());
                Log.d("buttonPressedID",buttonID);

                Log.d("isPositionEmpty", boardString);



                board[countI][countN].isPositionEmpty = false;

非常感谢帮助!提前谢谢!

2 个答案:

答案 0 :(得分:1)

看起来你混淆了你的x和y值? board [countN] [countY]的顺序与构建2-d数组的顺序相同。

board[countN][countI].isPositionEmpty = false;

你只有你的代码的摘录,但我会创建一个类来跟踪扩展ImageView / ImageButton的每个方块。然后它将自己处理,你不必自己设置 - 逻辑将在课堂上作为实际填充广场的行动的副作用。例如,你可以调用一个方法recordMove(int move),它既可以是玩家一个,也可以是玩家二。然后在类中处理所有更改图像并查看它是否为空的逻辑。

然后你只需要在嵌套循环中创建自定义按钮/视图,他们就会照顾好自己。也可以传入一个听众 - 无需制作64个匿名听众。

当你以这种方式组织它时很难弄乱它。

答案 1 :(得分:0)

我不知道为什么这个特例会失败。通常,您的代码似乎混合了很多数据和UI。我会尝试从视图中分离数据(例如,构成您的主板的所有数据)。 写一个只存储状态信息的类。

在另一个类中,您可以根据视图中的数据绘制所有视图。可能让你开始的是伪和代码的以下混合。它远非完整,但设置这样的结构,将来会阻止更多的问题。好运

class Square
{
    int buttonId;
    int rownum;
    int colnum;
    int state;    // can be empty || white || black
    //.. maybe more things you want to store   

    // some functions:
    void addPiece();
    void removePiece();
    void setButtonId(int id);

}

class Board
{
    // 2D array of Squares e.g. ArrayList<ArrayList<Square>>

    // some functions:
    void createEmptyBoard()    // which creates an empty board    
    ArrayList<ArrayList<Square> getAllSquares();
    ArrayList<Square> getAllWhiteSquare();
    ArrayList<Square> getAllBlackSquares();

}


/**
* You Activity should be able to draw the board, without any knowledge of the 
* underlying data. Vice Versa: your Board class, should know exactly where all
* pieces are, and what color they have, but should not care about what happens
* when they are clicked. Or, if they can be clicked.
*
* Your activity only has to make sure, that if views are clicked/dragged or moved
* that the Board class will be updated accordingly
*/
class YourActivity
{
    Board board;

    // some methods:
    drawBoard(Board board)
    {
        ArrayList<Square> white = board.getAllWhiteSquares();
        ArrayList<Square> black = board.getAllBlackSquares();

        for(Square w : white){
            Button b = new Button();    // create a button
            w.setButtonId(b.getId());   // store the button id in the square

            b.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    Square clickedSquare = board.getSquareWithId(v.getId());
                }
            );
        }
    }
}