2012-02-01 5 views
0

基本的には、アンドロイド用のリバーシアプリを実装していますが、現在は2次元配列の要素を更新しようとしています。コールはonClickListenerにあり、これはループ内にあり、リバーシボードを設定するために使用されました。問題は、ピースが配置されると、要素isPositionEmptyがfalseに変更されるはずですが、そうでないことです。コードのスニペットは次のとおりです。ループ内にあるボタンonClickListenerの配列要素を更新する - android

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; 

ご協力いただきありがとうございます。前もって感謝します!

答えて

0

この特定のケースがなぜ失敗するのかわかりません。一般的に、あなたのコードはデータとUIをたくさん組み合わせているようです。私はビューからデータ(例えばあなたのボードを構成するすべてのデータ)を分離しようとします。 状態に関する情報のみを格納するクラスを作成します。

別のクラスよりも、ビュー内のデータに基づいてすべてのビューを描画できます。あなたが始めるかもしれない何かは、擬似とコードの次の組み合わせです。これは完全ではありませんが、このような構造を設定することで、今後さらに多くの問題を防ぐことができます。 GOOD LUCK

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()); 
       } 
      ); 
     } 
    } 
} 
+0

助けてくれてありがとう、私は間違いなくこれを考慮に入れよう! – hazard1994

1

xとyの値が混ざっているようですか? board [countN] [countY]は、2次元配列を作成したのと同じ順序になります。

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

あなただけのあなたのコードの抜粋がありますが、私はImageViewの/のImageButtonを拡張し、各四角形を追跡するために、クラスになるだろう。それから、それは自分自身を世話し、あなた自身でそれを設定する必要はありません - ロジックは、実際に正方形を埋めるアクションの副作用としてクラスになります。たとえば、player1またはplayer2のいずれかのメソッドrecordMove(int move)を呼び出すことができます。次に、画像を変更して空であるかどうかを確認するすべてのロジックがクラス内で処理されます。

次に、ネストされたループでカスタムボタン/ビューを作成するだけで、自分自身の面倒を見ることになります。 1人の聴取者にも渡します。匿名の聴取者64人を作る必要はありません。

あなたがこのように整理すると、それを台無しにするのは難しいです。

関連する問題