denn1s
denn1s

Reputation: 155

Updating TextView from DatePicker

I am trying to update a TextView from inside a Datepicker, however I am getting the error

"non-static method (updateDateTextView(long)) can not be referenced from a static context"

Here is my DatePickerFragment.java

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

    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar calendar = new GregorianCalendar(year, month, day);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
        AddItem.updateDateTextView(timestamp);
    }
}

Here is my function inside AddItem.java which is an Activity.

      public  class AddItem extends AppCompatActivity {

    Context ctx = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }
    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    public void updateDateTextView(long timestamp){
        TextView textViewToChange = (TextView) findViewById(R.id.openDate);
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(timestamp);
        String date = DateFormat.format("dd-MM-yyyy", cal).toString();
        textViewToChange.setText(date);
    }

}

Upvotes: 1

Views: 825

Answers (4)

Nsubuga
Nsubuga

Reputation: 71

Alternatively one can define an interface in the DatePickerFragment class as in the code bellow.

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

DateChooserListenerHandler dateChooserListenerHandler;
@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 datePicker, int year, int month, int day) {
    Calendar calendar = new GregorianCalendar(year, month, day);
    long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
    //Note this is where the updateTextView method in the interface is invoked from
    ((DateChooserListenerHandler) getActivity()).updateTextView(timestamp);
}

//Define an interface with a method that takes long, it will be overridden by classes that implement the interface
public interface DateChooserListenerHandler{

    public void updateTextView(long s);
}

}

The AddItem activity class can then implement the the interface like in the code bellow.

public class AddItem extends AppCompatActivity implements DatePickerFragment.DateChooserListenerHandler {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}

//The overridden method from the interface defined in DatePickerFragment class
@Override
public void updateTextView(long s) {
    updateDateTextView(s);
}

public void updateDateTextView(long timestamp){
    TextView textViewToChange = (TextView) findViewById(R.id.textView);
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(timestamp);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();
    textViewToChange.setText(date);
}

}

That works fine for me however, I have not examined its performance implications on the app but I find it easier. In case you just wanted to update a TextView directly from the DatePickerFragment then you could do it like bellow.

 @Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
    Calendar calendar = new GregorianCalendar(year, month, day);
    long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
    TextView textView = (TextView) getActivity().findViewById(R.id.textView);
    textView.setText(timestamp + "");
}

Upvotes: 0

antonicg
antonicg

Reputation: 944

As they said you can't call non-static method like static method. Try this:

Implements the DatePickerDialog.OnDateSetListener and the logic in your Activity:

public class AddItem extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    showDatePickerDialog();
}

public void showDatePickerDialog() {
    DatePickerFragment newFragment = new DatePickerFragment();
    newFragment.init(this, this);
    newFragment.show(getSupportFragmentManager(), "date_picker");
}

public void updateDateTextView(long timestamp){
    TextView textViewToChange = (TextView) findViewById(R.id.openDate);
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(timestamp);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();
    textViewToChange.setText(date);
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

    Calendar calendar = new GregorianCalendar(year, monthOfYear, dayOfMonth);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());

        updateDateTextView(timestamp);
}

And initialize the DatePicker with the listener, the class should look like this:

public static class DatePickerFragment extends DialogFragment {

    DatePickerDialog.OnDateSetListener listener;
    Context context;

    public void init(Context context, DatePickerDialog.OnDateSetListener listener) {
        this.listener = listener;
        this.context = context;
    }

    @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(context, listener, year, month, day);
    }

}

I tried, and it works.

Upvotes: 2

You are doing this:

public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar calendar = new GregorianCalendar(year, month, day);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
        AddItem.updateDateTextView(timestamp); ///Here is the error
    }

the error is that AddITem.updateDateTextView is wrong. the method updateDateTextView is not static.

you need to start that Activity with an Intent and the somewhere in the lifecycle call the method updateDateTextView

or

modify the method and make it static (but that is not so good developer idea)

public static void onDateSet(DatePicker view, int year, int month, int day)

Upvotes: 0

1shubhamjoshi1
1shubhamjoshi1

Reputation: 556

A non-static method can not be used with a static class try to change the

public void updateDateTextView(long timestamp){ ..... }

to

public static void updateDateTextView(long timestamp){
...}

Upvotes: 0

Related Questions