0

I have a ViewPager using the Android support framework, which loads three fragments. The user can navigate between them by swiping.

The second fragment contains two GridViews, each filled with Buttons, using an Adapter.

The two GridViews are named (in XML) left_buttons and right_buttons. left_buttons is controlled by CategoryAdapter that extends BaseAdapter, and right_buttons is controlled by ItemAdapter that also extends BaseAdapter.

The problem that I'm having is that when a user clicks a button in CategoryAdapter, I would like to call notifyDataSetChanged() on ItemAdapter, followed by invalidateViews() on the associated GridView. Because the two adapters are created within the same fragment MenuFragment, I'm struggling to find the best way to communicate between them.

I've seen a lot of answers on communicating between fragments, and between fragments and activities, and so on. I tried getting a reference to R.id.right_buttons from within the CategoryAdapter, but couldn't find a solution (since no access to findViewById etc).

Then, I read some answers that suggested I need to create a method in the Fragment, so I tried the following in MenuFragment.class:

public void updateRightGrid() {

//grab a reference to the right hand grid right_buttons
GridView rightButtons = (GridView) getView().findViewById(R.id.right_buttons);

    //get the adapter that is controlling right_buttons
ItemAdapter menuItemAdapter = (ItemAdapter) rightButtons.getAdapter();

    //tell this adapter that we have some new data, so please refresh
menuItemAdapter.notifyDataSetChanged();

//force the grid to refresh
rightButtons.invalidateViews();

}

The problem is that I still can't find a way to call this method from the CategoryAdapter. Ive created an inner class that implements OnClickListener, and in public void onClick(View v) I'm trying to call that fragment's method updateRightGrid(). I have access to the context through the variable mContext, but that doesn't seem to help.

Any help would be much appreciated!

tmhumble
  • 5
  • 4

1 Answers1

0

You probably have context of your activity in your adapters, so you can make a method inside an activity like:

//your activity code
public void notifyItemAdapter() {
    itemAdapter.notifyDataSetChanged() ;
}

And then in your CategoryAdapter:

//adapter code
activityContext.notifyItemAdapter();
Michał Z.
  • 4,119
  • 1
  • 22
  • 32
  • The context is definitely there. I use: `leftButtons.setAdapter(new CategoryAdapter(this.getActivity()));` to pass the context. However, when I add a method to the FragmentActivity, like: `public void notifyItemAdapter() { Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show(); }` I get the following error in Eclipse: `The method notifyItemAdapter() is undefined for the type Context` - is this another Fragment thing? – tmhumble Jan 31 '13 at 20:49
  • OK - think I've fixed that - I needed to cast the context. So, I ended up calling the method with: `((MyFragmentActivity) mContext).notifyItemAdapter();` - seems OK so far. – tmhumble Jan 31 '13 at 21:32