0

MapCells と呼ばれるカスタム ImageButtons の GridLayout を生成して表示するアクティビティがあります (これはゲーム マップです。驚きです)。MapCells はランダムに生成され、地形の種類に関連するいくつかの変数と、見た目の違い (画像) が含まれています。コードを実行すると、素敵なランダム マップが表示されます。セルをクリックすると、画面上に ID が表示され (診断支援)、格納されている変数が取得され、setText でセルに設定されます。同じアクティビティ内のいくつかの TextViews。MapCells をクリックしたときに実際にどのように機能するかは、(予想どおり) 一意の ID をトーストし、独自の外観を持っていることですが、表示される地形値は、すべての場合に生成された最後の MapCell のものです。これは私の「for」の結果だと思いました すべてのループを上書きしていた変数 q (コメントアウトされたコードで表示) としてすべての MapCells を作成するループなので、以下のコードに書き直しましたが、同じ問題が発生しています。私が見た他の質問は、IDを与えることでこれが解決されることを示しているようです。何か案は?

package com.<redacted>

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class MapScreen extends Activity implements OnClickListener{
    private static int dimen = 110; //the side length of map cells
    private int currentFood;
    private String currentName;
    private MapCell[] storage = new MapCell[18];//keep the cells in here

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_screen); //the xml

  //It's Randy the randomizer!
    Random randy = new Random();

    //Generate 6 random MapCells per row for use in our map.
    //MapCell q; -this is old and unused now
    for(int i=0; i<=5; i++){
        //q = new MapCell(randy.nextInt(4), this); //create and randomize
        //q.setId(i); //issue identity
        storage[i] = new MapCell(randy.nextInt(4), this); //shove it in the array
        storage[i].setId(i);
        storage[i].setOnClickListener((OnClickListener) this);
        //q.setOnClickListener((OnClickListener) this);
        ((TableRow)findViewById(R.id.mapRow01)).addView(storage[i], dimen, dimen); //add cell to display
    }
    for(int j=0; j<=5; j++){
        //q = new MapCell(randy.nextInt(4), this);
        //q.setId(j+6);
        //storage[q.getId()] = q; //shove it in the array
        //q.setOnClickListener((OnClickListener) this);
        storage[j+6] = new MapCell(randy.nextInt(4), this); //shove it in the array
        storage[j+6].setId(j+6);
        storage[j+6].setOnClickListener((OnClickListener) this);
        ((TableRow)findViewById(R.id.mapRow02)).addView(storage[j+6], dimen, dimen); //add cell to display
    }
    for(int k=0; k<=5; k++){
        //q = new MapCell(randy.nextInt(4), this);
        //q.setId(k+12);
        //storage[q.getId()] = q; //shove it in the array
        //q.setOnClickListener((OnClickListener) this);
        storage[k+12] = new MapCell(randy.nextInt(4), this); //shove it in the array
        storage[k+12].setId(k+12);
        storage[k+12].setOnClickListener((OnClickListener) this);
        ((TableRow)findViewById(R.id.mapRow03)).addView(storage[k+12], dimen, dimen); //add cell to display
    }
}

public void displayCell(MapCell view){
    //get the cell name view and then set its text to be the name of the clicked cell
    try{ //we need to try in case it feeds in a non-MapCell view
        currentName = storage[view.getId()].getName();
        ((TextView)findViewById(R.id.mapDisplayName)).setText(currentName);
        currentFood = storage[view.getId()].getFood();
        ((TextView)findViewById(R.id.mapDisplayFood)).setText(String.valueOf(currentFood));
        Toast.makeText(this, "Cell " + String.valueOf(view.getId()), Toast.LENGTH_SHORT).show(); //diagnostic line
    }
    catch (Exception x){
        //frowny time
        Toast.makeText(this, "Something Broke on cell " + String.valueOf(view.getId()) + " :(", Toast.LENGTH_SHORT).show();
    }
}

public void onClick(View v){//when we click a MapCell
    displayCell((MapCell)v);// feed it in
}

}

それが該当するクラスです。ご要望があれば MapCell をお見せすることもできますが、これは getter を使用するためにいくつかの変数が追加された単なる ImageButton です。これらの変数は、コンストラクターで提供されるランダムな整数を使用して、大文字と小文字を切り替えて設定されます。

4

1 に答える 1

1

あなたのコードには何の問題も見当たらないので、暗闇の中で写真を撮ります。MapCellクラスで、静的モディファイヤを使用して定義された地形値を保持する変数はありますか?

于 2012-08-02T19:32:34.983 に答える