DialogFragment に追加する SugarORM データベースに格納されているオブジェクト (間隔) のリストがあります。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();
}
}
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