i_raqz
i_raqz

Reputation: 2959

android date picker from alert dialog

I am trying to start a date picker from an alert dialog. but for some reason the showDialog(DATE_DIALOG_ID) doesn't open up at all. Could some please tell me what is wrong.

final AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setIcon(R.drawable.icon);
        alt_bld.setTitle("Categories");
        alt_bld.setSingleChoiceItems(categoryType, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                System.out.println("Category selected= "+categoryType[item]);
                Toast.makeText(getApplicationContext(), "Category selected= "+categoryType[item], Toast.LENGTH_SHORT).show();
                category=categoryType[item];


                if(category.equalsIgnoreCase("Time")){
                    showDialog(DATE_DIALOG_ID);
                    System.out.println("selected date here:"+selectedDate);
                    Intent intent = new Intent(MainActivity.this, ListViewActivity.class);
                    bundle.putString("category", category);
                    bundle.putString("attribute", selectedDate);
                    intent.putExtras(bundle);
                    startActivity(intent);

                }

other datepicker related code

static final int DATE_DIALOG_ID = 0;



@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
        }
        return null;
    }   


    private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {


        public void onDateSet(DatePicker view, int year,
                              int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            selectedDate = Integer.toString(mMonth)+"-"+Integer.toString(mDay)+"-"+Integer.toString(mYear);
            System.out.println("selected date is:"+selectedDate);
        }


    };

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homescreen);

        final Calendar cal = Calendar.getInstance();
        mYear = cal.get(Calendar.YEAR);
        mMonth = cal.get(Calendar.MONTH);
        mDay = cal.get(Calendar.DAY_OF_MONTH);

Upvotes: 1

Views: 3905

Answers (1)

Varun
Varun

Reputation: 33983

In your code u are calling showDialog(int); and immediately launching a new intent. How do u expect to see a dialog. Infact there will be a memory leak caused because the dialog is not closed.

Upvotes: 1

Related Questions