我有以下OnCreateMethod()为什么我似乎无法从oncreate更新我的gridview?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_siege_main);
mContext = getApplicationContext();
gridview = (GridView) findViewById(R.id.gridview_board);
initialBoard = new Board();
initialBoard.generate(Globals.rand_width,
Globals.rand_height,
Globals.rand_low,
Globals.rand_high);
gridview.setAdapter(new BoardAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//updateGameState(initialBoard);
}
});
updateGameState(initialBoard);
//typography
TextView boardName = (TextView) findViewById(R.id.textview_board_name);
boardName.setText("Uncharted Territory");
Toast.makeText(SiegeMainActivity.this, "Uncharted Territory loaded", Toast.LENGTH_SHORT).show();
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/calibri-1.ttf");
boardName.setTypeface(face);
TextView pl1Name = (TextView) findViewById(R.id.textViewPlayer1Name);
face = Typeface.createFromAsset(getAssets(),
"fonts/calibri-1.ttf");
pl1Name.setTypeface(face);
TextView pl2Name = (TextView) findViewById(R.id.textViewPlayer2Name);
face = Typeface.createFromAsset(getAssets(),
"fonts/calibri-1.ttf");
pl2Name.setTypeface(face);
}
和我updateGameState()方法是这样的:
private void updateGameState(Board b) {
int [] vals = b.getLinearVals();
int [] plays = b.getLinearPlays();
int [] score = b.getScore();
int turns = b.getTurns();
Button tile;
//updating board
for(int i=0;i<gridview.getChildCount();i++){
tile = (Button)gridview.getChildAt(i);
tile.setText(""+vals[i]);
if(plays[i]==1)
tile.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_default_normal_green));
else if (plays[i]==2)
tile.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_default_normal_blue));
}
//Setting the score
Button pl1Score, pl2Score;
pl1Score = (Button) findViewById(R.id.buttonPlayer1Score);
pl2Score = (Button) findViewById(R.id.buttonPlayer2Score);
pl1Score.setText(""+score[0]);
pl2Score.setText(""+score[1]);
}
当我调用的OnCreate updateGameState()创建适配器后,应填写有一些值的按钮。但它似乎并不奏效。但是当我点击时做同样的事情(在点击监听器中注释掉),它似乎工作正常。我想在加载页面时填充值。有人能帮助我吗?
这不起作用。正如我所说,当我点击gridview时,它确实得到更新。当我执行你的,甚至不会发生 – aceBox 2014-10-20 15:02:05
嗯。检查编辑,看看是否有效。起初我没有注意到你直接在GridView中修改视图,而不是修改适配器将引用的视图列表(在我看来,这是正确的方式)。使用什么适合你=) – AnxGotta 2014-10-20 15:12:40