user2447290
user2447290

Reputation:

That I have wrong with this code? Trying to show a datepicker on a edittext

That I have wrong with this code? I date indicates that the variable has to be marked as final, but if I do it still does not work, shoot me error: Insert app indexed code api. I'm trying that through a EditText I display a datepicker

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

Here code

package com.example.cesar.mybankaccess.view;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import com.example.cesar.mybankaccess.R;

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

public class PromocionesFormulario extends AppCompatActivity{
    Calendar myCalendar;
    EditText fechaInicio;
    DatePickerDialog.OnDateSetListener date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_promociones_formulario);
        fechaInicio = (EditText) findViewById(R.id.fechaInicio);
        myCalendar = Calendar.getInstance();
        DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }
        };
        fechaInicio.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(PromocionesFormulario.class, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
    }


    private void updateLabel() {

        String myFormat = "MM/dd/yy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

        fechaInicio.setText(sdf.format(myCalendar.getTime()));
    }

}

Upvotes: 0

Views: 74

Answers (2)

kasapkiloet
kasapkiloet

Reputation: 330

UPDATE

As Msp mentioned, you also need to change that. Context is given with .this, not .class


If you're trying to make the object final, clear this line:

 DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { //...

and make it:

date = new DatePickerDialog.OnDateSetListener() { //...

then update your initialization, like:

final DatePickerDialog.OnDateSetListener date;

other than that, your question is not obvious.

Upvotes: 0

Msp
Msp

Reputation: 2493

Make DatePickerDialog.OnDateSetListener date final. And in,

new DatePickerDialog(PromocionesFormulario.class, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
    myCalendar.get(Calendar.DAY_OF_MONTH)).show();

Pass PromocionesFormulario.this instead of PromocionesFormulario.class.

The problem here is, the type of PromocionesFormulario.class is Class. But new DatePickerDialog() requires Context object as first parameter.

Upvotes: 0

Related Questions