Pergunta

I have two ImageButtons. One is responsible for setting a beginning date, and the other is responsible for setting the end date. When each one is pressed they display a DatePickerDialog. The onCreateDialog method is within another class. Here is that class:

DatePickerFragment.Java

package com.ThatOneNoob.smarthaul;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;


public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);


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


@Override
public void onDateSet(DatePicker v, int year, int month, int day) {
    // TODO Auto-generated method stub

    switch (v.getId()){

    case R.id.datesel1:
        toDate.setText(new StringBuffer());
    }
 }


}

Obviously incomplete, toDate is one of the TextViews it is suppose to manage. datesel1 is the ImageButton. It will append the TextView to include the set date in a 01/02/2013 format. I cannot declare a TextView as a static so I can't call it within this class. So, should I make the onDateSet invoke a static method that will set the TextView to what it needs to do? Or what should I do?

Foi útil?

Solução

Create the dialog in separate class and make a getter for the listener:

public class DatePickerFragment extends DialogFragment
{
    private OnDateSetListener onDateSetListener;

    public DatePickerFragment() {}

    public void setOnDateSetListener(OnDateSetListener onDateSetListener) {
        this.onDateSetListener = onDateSetListener;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), onDateSetListener, year, month, day);
    }

}

The implementation of DatePickerDialog.OnDateSetListener make in your activity of other fragment where you have reference to your TextView.
Then create a calendar and use SimpleDateFormat, see an example:

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, monthOfYear, dayOfMonth);
    <=== YOUR SETTEXT TO YOUR TEXTVIEW HERE ===>
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top