user6677709
user6677709

Reputation:

DatePicker is not working when i used Fragment

i set DatePicker it is working in Activity when i used Fragment it generating the errors like

Error:(63, 61) error: incompatible types: MainActivity cannot be converted to Context 

Here i paste the complete code.

MainActivity.Java

    package com.example.sachin.datepicker;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;
import java.util.Locale;

public class MainActivity extends Fragment implements View.OnClickListener {
        private EditText fromDateEtxt;
        private EditText toDateEtxt;
        private EditText toDateEtxt1;
        private DatePickerDialog fromDatePickerDialog;
        private DatePickerDialog toDatePickerDialog;
        private DatePickerDialog toDatePickerDialog1;
        private SimpleDateFormat dateFormatter;

        View view;
        @TargetApi(Build.VERSION_CODES.N)
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                View view = inflater.inflate(R.layout.activity_add_wod, container, false);
                dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

                findViewsById();

                setDateTimeField();
                return view;
        }

        private void findViewsById() {
                fromDateEtxt = (EditText) view.findViewById(R.id.openingdate);
                fromDateEtxt.setInputType(InputType.TYPE_NULL);
                fromDateEtxt.requestFocus();

                toDateEtxt = (EditText) view.findViewById(R.id.Birthdate);
                toDateEtxt.setInputType(InputType.TYPE_NULL);
                toDateEtxt.requestFocus();
                toDateEtxt1 = (EditText) view.findViewById(R.id.Anniversary);
                toDateEtxt1.setInputType(InputType.TYPE_NULL);
        }

        private void setDateTimeField() {
                fromDateEtxt.setOnClickListener(this);
                toDateEtxt.setOnClickListener(this);
                toDateEtxt1.setOnClickListener(this);
                Calendar newCalendar = Calendar.getInstance();


                fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Calendar newDate = Calendar.getInstance();
                                newDate.set(year, monthOfYear, dayOfMonth);
                                fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
                        }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

                toDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                        @TargetApi(Build.VERSION_CODES.N)
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Calendar newDate = Calendar.getInstance();
                                newDate.set(year, monthOfYear, dayOfMonth);
                                toDateEtxt.setText(dateFormatter.format(newDate.getTime()));
                        }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
                toDatePickerDialog1 = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                        @TargetApi(Build.VERSION_CODES.N)
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Calendar newDate = Calendar.getInstance();
                                newDate.set(year, monthOfYear, dayOfMonth);
                                toDateEtxt1.setText(dateFormatter.format(newDate.getTime()));
                        }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
        }



        @Override
        public void onClick(View view) {
                if(view == fromDateEtxt) {
                        fromDatePickerDialog.show();
                } else if(view == toDateEtxt) {
                        toDatePickerDialog.show();
                }  else if(view == toDateEtxt1) {
                                toDatePickerDialog1.show();
                }
        }
}

this code is not working:-

 fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            Calendar newDate = Calendar.getInstance();
                            newDate.set(year, monthOfYear, dayOfMonth);
                            fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
                    }

            },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

i tried it but it is generating the same error again and again..

Upvotes: 2

Views: 1023

Answers (2)

Ramesh Prajapati
Ramesh Prajapati

Reputation: 662

you extend fragment so replace this to getActivity()

fromDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {}

Upvotes: 2

earthw0rmjim
earthw0rmjim

Reputation: 19417

The constructor of DatePickerDialog expects a Context param.

You're passing your Fragment instance to the constructor of DatePickerDialog, but Fragment is not a Context subclass.

Either pass getContext() or getActivity() instead of this.

Change the following line:

fromDatePickerDialog = new DatePickerDialog(this,
    new DatePickerDialog.OnDateSetListener() {

To this:

fromDatePickerDialog = new DatePickerDialog(getContext(),
    new DatePickerDialog.OnDateSetListener() {

Or to this:

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

According to your code you have multiple DatePickerDialogs, so you need to perform this change on all of them.

P.S. Your Fragment should not be called MainActivity.

Upvotes: 1

Related Questions