1

I'm sorry if my question seems common. I have google and have many results, but none of them I can apply.

I have a fragment that uses GridView layout. I also use BaseAdapter for my gridview.

When the data changes, I call gridView.invalidateViews(); but gridview doesn't redraw with new data.

gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                               getTable(position).setStatus("busy");   // change data 
                               gridView.invalidateViews(); // and redraw
                }

Or, I do another way:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.waiter_view_fragment, container, false);

    gridView = (GridView) (rootView).findViewById(R.id.waiterGridView);
    gridView.setAdapter(new ImageAdapterHost(this, getActivity())); 
        // Add this line here
        ((BaseAdapter)gridView.getAdapter()).notifyDataSetChanged();
    }

The answer still be same. My gridview doesn't redrawn, although when I test data, the data really changes, but dont update my gridview.

Please teach me how to redraw gridview fragment after data change.

Thanks :)

hqt
  • 29,632
  • 51
  • 171
  • 250

1 Answers1

1

Keep your adapter as a local variable inside Fragment, add a method to your Fragment that calls my_adapter_variable.notifyDataSetChanged(). Once your data is changed find your fragment and call that method. Something like this works for me:

public void gotLessonDetails(LessonDetailsEvent event)
{
[...]
    for (String tag: tabs) {
        StudentListFragment f = (StudentListFragment) getSupportFragmentManager().findFragmentByTag(tag);
        if (f != null) {
            f.refreshAdapter();
        }
     }
[...]
}
pelotasplus
  • 9,852
  • 1
  • 35
  • 37
  • Data change when item of grid view has been clicked. So, each time item has been clicked, I change data and I call `((BaseAdapter)gridView.getAdapter()).notifyDataSetChanged();` inside its fragment. But nothing change :( Can I do that ? – hqt Mar 29 '13 at 20:58
  • Can you please assist? http://stackoverflow.com/questions/40095826/how-to-update-a-fragment-that-has-a-gridview-populated-from-sqlite – Si8 Oct 26 '16 at 23:19