Android DialogFragment不更新显示

Android DialogFragment不更新显示,android,android-dialogfragment,android-datepicker,Android,Android Dialogfragment,Android Datepicker,我是android开发新手,希望你能帮我解决我的问题。任何帮助都将不胜感激。我创建了一个用于挑选日期的对话框片段。我可以选择所需的日期,但在显示所选日期时遇到问题 这是我的对话片段 public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { private Calendar mCalendar; @Override public Dialog

我是android开发新手,希望你能帮我解决我的问题。任何帮助都将不胜感激。我创建了一个用于挑选日期的对话框片段。我可以选择所需的日期,但在显示所选日期时遇到问题

这是我的对话片段

public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {


private Calendar mCalendar;  

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker

     mCalendar = Calendar.getInstance();
    int year = mCalendar.get(Calendar.YEAR);
    int monthOfYear = mCalendar.get(Calendar.MONTH);
    int dayOfMonth = mCalendar.get(Calendar.DAY_OF_MONTH);



    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, monthOfYear, dayOfMonth);
}


@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    mCalendar.set(Calendar.YEAR, year);
    mCalendar.set(Calendar.MONTH, monthOfYear);
    mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

}
我从我的提醒片段中调用它,它扩展了片段

   private void registerButtonListenersAndSetDefaultText() {

    mDateButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //getActivity().showDialog(DATE_PICKER_DIALOG);
            android.support.v4.app.DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getFragmentManager(), "datePicker");
            updateDateButtonText();
        }
    }); 



      updateDateButtonText();
}
和我的更新按钮:

private void updateDateButtonText() {
    // Set the date button text based upon the value from the database 
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 
    String dateForButton = dateFormat.format(mCalendar.getTime()); 
    mDateButton.setText(dateForButton);
}
我希望你能帮助我。我已经想了好几天了,但是我没有运气。提前谢谢

这是我的DialogFragment的完整代码。我希望这将有助于:

 package com.csu.eclassrecord;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener{


        // 
        // Date Format 
        //
        private static final String DATE_FORMAT = "yyyy-MM-dd"; 
        private static final String TIME_FORMAT = "kk:mm";
        public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";

        private EditText mTitleText;
        private EditText mBodyText;
        private Button mDateButton;
        private Button mTimeButton;
        private Button mConfirmButton;
        private Long mRowId;
        private RemindersDbAdapter mDbHelper;
        private Calendar mCalendar;  



    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker

         mCalendar = Calendar.getInstance();
        int year = mCalendar.get(Calendar.YEAR);
        int monthOfYear = mCalendar.get(Calendar.MONTH);
        int dayOfMonth = mCalendar.get(Calendar.DAY_OF_MONTH);



        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, monthOfYear, dayOfMonth);
    }



    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, monthOfYear);
        mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateDateButtonText();
    }
    private void updateDateButtonText() {
        // Set the date button text based upon the value from the database 
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 
        String dateForButton = dateFormat.format(mCalendar.getTime()); 
        mDateButton.setText(dateForButton);
    }
}
在我的提醒中:

package com.csu.eclassrecord;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.csu.eclassrecord.R;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class RemindersFragment extends Fragment {

    // 
    // Dialog Constants
    //
    private static final int DATE_PICKER_DIALOG = 0;
    private static final int TIME_PICKER_DIALOG = 1;

    // 
    // Date Format 
    //



    private static final String DATE_FORMAT = "yyyy-MM-dd"; 
    private static final String TIME_FORMAT = "kk:mm";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";

    private EditText mTitleText;
    private EditText mBodyText;
    private Button mDateButton;
    private Button mTimeButton;
    private Button mConfirmButton;
    private Long mRowId;
    private RemindersDbAdapter mDbHelper;
    private Calendar mCalendar;  

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         mDbHelper = new RemindersDbAdapter(getActivity());

        View rootView = inflater.inflate(R.layout.fragment_reminders, container, false);
          mCalendar = Calendar.getInstance(); 
            mTitleText = (EditText) rootView.findViewById(R.id.title);
            mBodyText = (EditText) rootView.findViewById(R.id.body);
            mDateButton = (Button) rootView.findViewById(R.id.reminder_date);
            mTimeButton = (Button) rootView.findViewById(R.id.reminder_time);

            mConfirmButton = (Button) rootView.findViewById(R.id.confirm);

            mRowId = savedInstanceState != null ? savedInstanceState.getLong(RemindersDbAdapter.KEY_ROWID) 
                                                : null;

            registerButtonListenersAndSetDefaultText();
        return rootView;
    }

    private void setRowIdFromIntent() {
        if (mRowId == null) {
            Bundle extras = getActivity().getIntent().getExtras();            
            mRowId = extras != null ? extras.getLong(RemindersDbAdapter.KEY_ROWID) 
                                    : null;

        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mDbHelper.close(); 
    }

    @Override
    public void onResume() {
        super.onResume();
        mDbHelper.open(); 
        setRowIdFromIntent();
        populateFields();
    }
    /**
    protected Dialog onCreateDialog(int id) {
        switch(id) {
            case DATE_PICKER_DIALOG: 
                return showDatePicker();
            case TIME_PICKER_DIALOG: 
                return showTimePicker(); 
        }
        return super.onCreate(id);



private DatePickerDialog showDatePicker() {


        DatePickerDialog datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                mCalendar.set(Calendar.YEAR, year);
                mCalendar.set(Calendar.MONTH, monthOfYear);
                mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateDateButtonText(); 
            }
            }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)); 
        return datePicker; 
    }
        **/

   private TimePickerDialog showTimePicker() {

        TimePickerDialog timePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                mCalendar.set(Calendar.MINUTE, minute); 
                updateTimeButtonText(); 
            }
        }, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), true); 

        return timePicker; 
    }


    private void registerButtonListenersAndSetDefaultText() {

        mDateButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //getActivity().showDialog(DATE_PICKER_DIALOG);
                android.support.v4.app.DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
            }
        }); 


        mTimeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                getActivity().showDialog(TIME_PICKER_DIALOG); 
            }
        }); 

        mConfirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent;
                saveState(); 
                getActivity().setResult(Activity.RESULT_OK);
                Toast.makeText(getActivity(), getString(R.string.task_saved_message), Toast.LENGTH_SHORT).show();
                intent = new Intent(getActivity().getApplication(), Manage_Settings.class);
                startActivity(intent);
            }

        });

          updateDateButtonText(); 
          updateTimeButtonText();
    }

    private void populateFields()  {



        // Only populate the text boxes and change the calendar date
        // if the row is not null from the database. 
        if (mRowId != null) {
            Cursor reminder = mDbHelper.fetchReminder(mRowId);
            getActivity().startManagingCursor(reminder);
            mTitleText.setText(reminder.getString(
                    reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_TITLE)));
            mBodyText.setText(reminder.getString(
                    reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_BODY)));


            // Get the date from the database and format it for our use. 
            SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
            Date date = null;
            try {
                String dateString = reminder.getString(reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_DATE_TIME)); 
                date = dateTimeFormat.parse(dateString);
                mCalendar.setTime(date); 
            } catch (ParseException e) {
                Log.e("ReminderEditActivity", e.getMessage(), e); 
            } 
        } else {
            // This is a new task - add defaults from preferences if set. 
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
            String defaultTitleKey = getString(R.string.pref_task_title_key); 
            String defaultTimeKey = getString(R.string.pref_default_time_from_now_key); 

            String defaultTitle = prefs.getString(defaultTitleKey, null);
            String defaultTime = prefs.getString(defaultTimeKey, null); 

            if(defaultTitle != null)
                mTitleText.setText(defaultTitle); 

            if(defaultTime != null)
                mCalendar.add(Calendar.MINUTE, Integer.parseInt(defaultTime));

        }

        updateDateButtonText(); 
        updateTimeButtonText(); 

    }

    private void updateTimeButtonText() {
        // Set the time button text based upon the value from the database
        SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT); 
        String timeForButton = timeFormat.format(mCalendar.getTime()); 
        mTimeButton.setText(timeForButton);
    }

    private void updateDateButtonText() {
        // Set the date button text based upon the value from the database 
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 
        String dateForButton = dateFormat.format(mCalendar.getTime()); 
        mDateButton.setText(dateForButton);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(RemindersDbAdapter.KEY_ROWID, mRowId);
    }



    private void saveState() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();

        SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT); 
        String reminderDateTime = dateTimeFormat.format(mCalendar.getTime());

        if (mRowId == null) {

            long id = mDbHelper.createReminder(title, body, reminderDateTime);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateReminder(mRowId, title, body, reminderDateTime);
        }

        new ReminderManager(getActivity()).setReminder(mRowId, mCalendar); 
    }


}

在我的提醒片段上的oncreateview和我的对话框FragmentPrivate static final String DATE_FORMAT=“yyyy-MM-dd”上的oncreatedialog之前,您什么时候设置了日期格式;私有静态最终字符串时间_FORMAT=“kk:mm”;公共静态最终字符串日期\时间\格式=“yyyy-MM-dd-kk:MM:ss”;在
UpdateDataButtonText
中,在我的提醒片段上的oncreateview之前,何时初始化is
McAllendar