Android:动态生成的ImageButtons数组只返回上次单击时创建的ImageButtons的信息

Android:动态生成的ImageButtons数组只返回上次单击时创建的ImageButtons的信息,android,dynamic,for-loop,settext,Android,Dynamic,For Loop,Settext,我有一个活动,它生成并显示一个称为MapCells的自定义图像按钮的网格布局(这是一个游戏地图,令人惊讶)。MapCells是随机生成的,包含一些与地形类型相关的变量,以及看起来不同的变量(图像)。当我运行我的代码时,我得到了我可爱的随机映射,当我点击单元格时,它们应该在屏幕上显示它们的id(一种诊断帮助),并获取它们存储的变量,并将它们设置为同一活动中的一些文本视图。当我点击地图单元时,它的实际工作原理是它们具有唯一的id(如预期的那样)并具有唯一的外观,但显示的地形值是在所有情况下生成的最后

我有一个活动,它生成并显示一个称为MapCells的自定义图像按钮的网格布局(这是一个游戏地图,令人惊讶)。MapCells是随机生成的,包含一些与地形类型相关的变量,以及看起来不同的变量(图像)。当我运行我的代码时,我得到了我可爱的随机映射,当我点击单元格时,它们应该在屏幕上显示它们的id(一种诊断帮助),并获取它们存储的变量,并将它们设置为同一活动中的一些文本视图。当我点击地图单元时,它的实际工作原理是它们具有唯一的id(如预期的那样)并具有唯一的外观,但显示的地形值是在所有情况下生成的最后一个地图单元的值。我认为这是我的“for”循环将所有mapcell创建为变量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
}
包com。
导入java.util.Random;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.TableRow;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MapScreen扩展活动实现OnClickListener{
私有静态int dimen=110;//映射单元的边长
私人食品;
私有字符串currentName;
private MapCell[]storage=new MapCell[18];//将单元格保存在此处
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map_屏幕);//xml
//是随机发生器兰迪!
随机随机数=新随机数();
//每行生成6个随机映射单元格,用于我们的映射。
//MapCell q;-这是旧的且未使用的

对于(int i=0;i,由于我认为代码没有问题,我将在黑暗中拍摄。在MapCell类中,保存地形值的变量是否使用static修饰符定义