wirthual
wirthual

Reputation: 117

Refresh Fragment from AlertDialog

I have a Fragment with a ListView on it. This ListView is filled with an adapter.

I start a DialogFragment an there I can type in the data for a now row on the ListView.

Now I want to refresh the Fragment under the Dialog when the Dialog is closed.

How can I refresh the ListView? I can't call the notifyDataSetChanged() from outside the fragment.

I read something about a callback loader, but I populate the ListView with an array. As far as I understand the callback Loader i have to use a Cursor for that.

I also tried to call a function of the activity but then i couldn't call the notifyDataSetChanged() too. To make the Adapter static didn't work.

I Hope someone understands my Problem and can halp to fix it.

Upvotes: 0

Views: 1598

Answers (2)

SimonSays
SimonSays

Reputation: 10977

You could just set the calling fragment as listener.

FragmentManager fm = getSupportFragmentManager();
MyDialog d = new MyDialog();
d.setDataChangeListener(this);
d.show(fm, "fragment_name");

Upvotes: 0

vRallev
vRallev

Reputation: 5040

In your case I would pass the Adapter instance to the class, which creates the Dialog. I don't see there any other solution.

You can also create the Dialog in the Fragment class and define the OnClickListener as inner class, so you can directly access the Adapter.

You can also use something like an event bus, which helps a lot for louse coupling.

Upvotes: 1

Related Questions