I have a list of objects (Intervals) stored in a SugarORM database that I'm adding to in a DialogFragment. The DialogFragment is shown from the MainActiviy and saves and creates the object in the DialogFragment itself and which then calls the DialogFragment's dialoglistener interface which communicates back to the MainActivity that then notify's the ListFragment (which is displayed in a tabbed ViewPager) that it should refresh the list. This, however, does not happen. The list does update after the app resumes or restarts
I've looked here How to refresh the ListView after change the data with the DialogFragment? and here Receive result from DialogFragment With no success.
Thanks in advance!
Dialog Fragment code
public class NewIntervalDialogFragment extends android.support.v4.app.DialogFragment {
private Button btnSave;
private EditText etLabel;
public NewIntervalDialogFragment() {
// Required empty public constructor
}
public static NewIntervalDialogFragment newInstance() {
return new NewIntervalDialogFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View newView = inflater.inflate(R.layout.fragment_new_interval_dialog, container, false);
//initiate all Variable and set up view
btnSave = (Button) newView.findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditDialogListener activity = (EditDialogListener) getActivity();
if(activity==null){
Log.d("NULL" ,"NULL ACTIVITY");
}
long workMins = npMinutesWork.getValue() * 60000;
long restMins = npMinutesRest.getValue() * 60000;
long workSecs = npSecondsWork.getValue() * 1000;
long restSecs = npSecondsRest.getValue() * 1000;
String label = etLabel.getText().toString();
Interval newInterval = new Interval(label, workMins + workSecs, restMins + restSecs);
newInterval.save();
activity.onFinishEditDialog();
dismiss();
}
});
return newView;
}
public interface EditDialogListener {
void onFinishEditDialog();
}
}
MainActivity
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener, IntervalListFragment.OnListFragmentInteractionListener, NewIntervalDialogFragment.EditDialogListener{
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
static final String INTERVALS_KEY = "Saved Intervals";
static final String WORKOUT_KEY = "Current Workout";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton mFab = new FloatingActionButton.Builder(this)
.withColor(getResources().getColor(R.color.logo_color))
.withDrawable(getResources().getDrawable(R.drawable.plus))
.withSize(72)
.withMargins(0, 0, 16, 16)
.create();
mFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
final ActionBar actionBar = getSupportActionBar();
getSupportActionBar().setDisplayUseLogoEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), this);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(mSectionsPagerAdapter.getItem(0), INTERVALS_KEY).commit();
fragmentManager.beginTransaction().add(mSectionsPagerAdapter.getItem(1), WORKOUT_KEY).commit();
}
void showDialog(){
final DialogFragment newFragment = NewIntervalDialogFragment.newInstance();
newFragment.show(getSupportFragmentManager(), "dialog");
}
@Override
public void onFinishEditDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
IntervalListFragment fragment = (IntervalListFragment) fragmentManager.findFragmentByTag(INTERVALS_KEY);
fragment.refresh();
}
}
ListFragment Code
public class IntervalListFragment extends android.support.v4.app.ListFragment {
private OnListFragmentInteractionListener mListener;
public int DIALOG_REQUEST_CODE = 101;
private ArrayList<Interval> intervals;
private IntervalListAdapter adapter;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public IntervalListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
intervals = (ArrayList<Interval>) Interval.listAll(Interval.class);}
catch (android.database.sqlite.SQLiteException e){
intervals = new ArrayList<>(10);
}
//Interval example = new Interval("Situps", 10000L, 50000L);
//intervals.add(example);
adapter = new IntervalListAdapter(getActivity(), intervals);
setListAdapter(adapter);
}
public void refresh(){
Log.d("REFRESH", "REFRESH CALLED");
adapter.clear();
adapter.refresh(Interval.listAll(Interval.class));
}
ListAdapter Code
public class IntervalListAdapter extends ArrayAdapter<Interval> {
public void refresh(List<Interval> intervals)
{
this.intervals = intervals;
notifyDataSetChanged();
}e