0

My GridView is created in my onCreate here:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));
...

My ImageAdapter class contains an int[]"images" of R.drawable. references to .png files. I have an onItemClickListener further down in my onCreate that will, depending on where and when my gridview is touched, will update the "images" array. It looks like this, with a lot of extraneous code stripped out:

...
gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                while (count <= 6) {
...
                    if (count == 2 && verifyEmpty == false
                            && choosePiece == true && chooseEmpty == true) {
                        verifyEmpty = gc.checkEmptyChoice(position);
                        if (verifyEmpty == true) {
                            gc.moveToEmpty(position, gameBoardModel,
                                    (ImageView) v, selectedLocation);
                            count++;
                            // UPDATE THE GRIDVIEW SOMEHOW HERE.
                        }
                        break;
                    }
...

I want to update my gridview right after the count++ inside the nested if. The method right above it, gc.moveToEmpty(...) will make changes to the images array and I want those changes reflected in the gridview. I have attempted using notifyDataSetChanged() and invalidateViews but cannot make either of them work from inside my onItemClickListener.

-------------------SOLUTION--------------FROM ANSWER BELOW-----------------

Had to add a name to the ImageAdapter:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageAdapter iA = new ImageAdapter(this);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(iA);
...

needed to be final so the the onClick... could use it. Then I could reference it like so:

                    if (verifyEmpty == true) {
                        gc.moveToEmpty(position, gameBoardModel,
                                (ImageView) v, selectedLocation);
                        count++;
                        // UPDATE THE GRIDVIEW SOMEHOW HERE.
                        // LIKE THIS!
                        iA.notifyDataSetChanged();
                    }
ChrisWilson4
  • 195
  • 4
  • 23

1 Answers1

2

Save a reference to your ImageAdapter and then call notifyDataSetChanged:

imageAdapter.notifyDataSetChanged()

Which notifies the adapter that the underlying data has changed and it should refresh.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443