0

我有一个活动,它生成并显示名为 MapCells 的自定义 ImageButtons 的 GridLayout(这是一个游戏地图,令人惊讶。) MapCells 是随机生成的,包含一些与地形类型相关的变量以及看起来不同的变量(图像)。当我运行我的代码时会发生什么,我得到了我可爱的随机地图,当我点击单元格时,他们应该在屏幕上烤他们的 id(诊断帮助)以及获取他们存储的变量并将它们设置为同一活动中的一些 TextView。当我单击 MapCells 时,它的实际工作方式是它们烘烤唯一的 ID(如预期的那样)并具有其独特的外观,但显示的地形值是在所有情况下生成的最后一个 MapCell。我以为这是我的“for”的结果 循环创建所有 MapCells 作为变量 q (在注释掉的代码中可见),我只是覆盖了每个循环,所以我将它重写为下面的代码,但是,我遇到了同样的问题。我看过的其他问题似乎表明给他们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,但它只是一个 ImageButton,带有一些添加的变量,以便与 getter 一起使用。这些变量由 switch case 使用构造函数中提供的随机整数设置。

4

1 回答 1

1

由于我认为您的代码没有任何问题,因此我将在黑暗中试一试。在您的 MapCell 类中,保存地形值的变量是否使用静态修饰符定义?

于 2012-08-02T19:32:34.983 回答