在DialogFragment中添加后,ListView不会刷新

时间:2015-03-14 23:47:39

标签: android android-fragments android-activity sugarorm

我有一个存储在SugarORM数据库中的对象列表(Intervals),我在DialogFragment中添加它。 DialogFragment显示在MainActiviy中,并在DialogFragment本身中保存并创建对象,然后调用DialogFragment的dialoglistener接口,该接口与MainActivity进行通信,然后通知ListFragment(显示在一个ListFragment中)选项卡式ViewPager)它应该刷新列表。然而,这不会发生。应用程序恢复或重新启动后,列表会更新

我看过这里 How to refresh the ListView after change the data with the DialogFragment? 和这里 Receive result from DialogFragment 没有成功。

提前致谢!

对话框片段代码

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代码

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代码

public class IntervalListAdapter extends ArrayAdapter<Interval> {

public void refresh(List<Interval> intervals)
{
    this.intervals = intervals;
    notifyDataSetChanged();
}e

1 个答案:

答案 0 :(得分:0)

添加代码

 adapter.setNotifyDataChanged()