我的MapCells是隨機生成,生成並顯示被稱爲MapCells定製ImageButtons的網格佈局(這是一個遊戲地圖,驚喜)的活動,幷包含一些與地形類型相關的變量以及看起來不同的圖像。什麼時候我的代碼運行情況是,我讓我的可愛的隨機地圖,當我點擊的細胞,他們應該敬酒,他們的ID屏幕(輔助診斷)上,以及獲得它們所存儲的變量,並將它們的setText-ING於一些TextViews在同一個活動中。如何當我點擊MapCells它的實際工作是他們敬酒的唯一ID(如預期),並有其獨特的外觀,但顯示的地形值是在所有的情況下產生的最後MapCell的。我認爲這是我的「for」循環創建所有MapCells作爲變量的q(在可見註釋掉的代碼),我只是覆蓋每一個循環的結果,所以我把它改寫了下面的代碼,但是,我得到一樣的問題。我看過的其他問題似乎表明,給他們ID將解決這個問題。有任何想法嗎?安卓:動態生成ImageButtons數組只能從創建的最後一個點擊後返回信息
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搭配。這些變量由開關大小寫使用構造函數中提供的隨機整數設置。