为什么我似乎无法从oncreate更新我的gridview?

时间:2014-10-20 14:33:45

标签: java android gridview

我有以下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()时,它应该用一些值填充按钮。但它似乎没有用。但是当我在点击上做同样的事情(在点击监听器中注释掉)时,它似乎工作正常。我想在加载页面时填充值。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

您需要让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并且它不适合您...也许可以尝试 this ,表示使用GridView s invalidateViews()方法。 gridView.invalidateViews()

答案 1 :(得分:0)

看看这个问题&amp;在这里回答:

How to refresh a GridView?

我遇到了类似的问题,但是即使这些项目被更改和添加,也无法通过点击按钮进行刷新。在我的例子中,结果是,使用新的/更新的项目列表重新创建(在您的情况下)BoardAdapter,然后将其重新分配给gridview。

gridViewAdapter = new MyGridViewAdapter(this, blah, yourUpdatedItems);
discoverGridView.InvalidateViews();
discoverGridView.Adapter = gridViewAdapter;

我确信必须有一种更好,更具反应性的方法,但这至少可以使我的概念证明正常运行。