0

I have the following 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);

}

and my updateGameState() method is like this:

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]);
}

When I call updateGameState() in OnCreate after creating the adapter, it should fill the buttons with some values. But it doesn't seem to work. But when I do the same thing on a click (commented out in the click listener), it seems to work fine. I want to populate the values on loading the page. Can somebody help me?

aceBox
  • 711
  • 2
  • 13
  • 30

2 Answers2

0

You need to let the GridView know that the data has changed. If you are dynamically changing the data in the ArrayAdapter, you need to call BoardAdapter.notifyDataSetChanged() so the GridView can "refresh" itself with the new data.

In your case, you need to create a reference to you new BoardAdapter and then use that to call the notifyDataSetChange() method.

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();
}

Edit: You mentioned in the comment that you tried using an ArrayAdapter and it didn't work for you... maybe try this, which says to use the GridViews invalidateViews() method. gridView.invalidateViews()

Community
  • 1
  • 1
AnxGotta
  • 1,006
  • 7
  • 28
  • this doesn't work. As I said, when i click on the gridview, then it does get updated. When I execute yours, even that doesn't happen – aceBox Oct 20 '14 at 15:02
  • Hmm. Check the edit and see if that works. I didn't notice at first that you are modifying the views directly in the GridView rather than modifying the List of views that the adapter would reference (Which is the proper way, in my opinion). Use what works for you =) – AnxGotta Oct 20 '14 at 15:12
0

Have a look at this SO question & answer here:

How to refresh a GridView?

I had a similar problem, but couldn't get it to even refresh on a button click, even tho items were being changed and added. In my case, the trick, as it turns out, was to recreate the (in your case) BoardAdapter with the new/updated list of items, and then reassign it to the gridview.

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

I am sure there has to be a better, more reactive, way of doing it, but this at least got my proof of concept up and running.

Community
  • 1
  • 1
wislon
  • 723
  • 5
  • 21