0

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

我看过这里 如何在使用 DialogFragment 更改数据后刷新 ListView? 在这里 从 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();
}

}

主要活动

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();

    }
}

列表片段代码

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));

}

列表适配器代码

public class IntervalListAdapter extends ArrayAdapter<Interval> {

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

1 回答 1

0

添加代码

 adapter.setNotifyDataChanged()
于 2015-03-15T03:29:50.813 回答