[英]Why I can't seem to update my gridview from oncreate?
我有以下OnCreateMethod()
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
知道数据已更改。 如果要动态更改ArrayAdapter
的数据,则需要调用BoardAdapter.notifyDataSetChanged()
以便GridView
可以使用新数据“刷新”自身。
在您的情况下,您需要创建对新BoardAdapter
的引用,然后使用该引用来调用notifyDataSetChange()
方法。
private BoardAdapter boardAdapter = null;
protected void onCreate(Bundle savedInstanceState) {
// your things
boardAdapter = new BoardAdapter(this);
gridview.setAdapter(boardAdapter);
// more things
}
private void updateGameState(Board b) {
// update your data
boardAdapter.notifyDataSetChanged();
}
编辑:您在注释中提到您尝试使用ArrayAdapter
,但它对您不起作用...也许尝试一下 ,它说使用GridView
的invalidateViews()
方法。 gridView.invalidateViews()
在这里看看这个问题和答案:
我有一个类似的问题,但是即使更改和添加了项目,也无法在单击按钮时刷新它。 就我而言,结果就是,用新的/更新的项目列表重新创建(在您的情况下)BoardAdapter,然后将其重新分配给gridview。
gridViewAdapter = new MyGridViewAdapter(this, blah, yourUpdatedItems);
discoverGridView.InvalidateViews();
discoverGridView.Adapter = gridViewAdapter;
我确信必须有一个更好的,更被动的方法,但这至少使我的概念证明得以运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.