What is the reason my date picker is stopping before I click on Set Date?

65 views Asked by At

I have used this datepicker but here my new activity is starting before I click on the set date button here is the code below:-

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Button;
import android.widget.ImageButton;

/**
 *To handle the main UI.
 */
public class ui extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    StringBuffer sb = new StringBuffer();
    ImageButton btn;
    int year_x,month_x,day_x;
    final int DIALOG_ID=0;
    public void showDialogOnButtonClick(){
        btn=(ImageButton)findViewById(R.id.bread);
        btn.setOnClickListener(
                    new View.OnClickListener(){
                        @Override
                        public void onClick(View v){
                            showDialog(DIALOG_ID);
                            Intent j=new Intent(ui.this,Read.class);
                            j.putExtra("date",sb.toString());
                            startActivity(j);
                        }
                    }
            );
    }
        protected Dialog onCreateDialog(int id){
            if(id==DIALOG_ID)
                return new DatePickerDialog(this, dPickerListener ,year_x,month_x,day_x);
            return null;
        }
        private DatePickerDialog.OnDateSetListener dPickerListener=new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                year_x=year;
                month_x=monthOfYear+1;
                day_x=dayOfMonth;
                sb.append(day_x).append(month_x).append(year_x);
            }
        };

   public void onwriteclick(View v){
       if(v.getId()==R.id.bwrite){
           showDialog(DIALOG_ID);
           Intent j=new Intent(ui.this,write.class);
           j.putExtra("date",sb.toString());
           startActivity(j);
       }
   }

    public void onpdfclick(View v){
        if(v.getId()==R.id.bpdf){
            showDialog(DIALOG_ID);
            Intent j=new Intent(ui.this,noteselectorforpdf.class);
            j.putExtra("date",sb.toString());
            startActivity(j);
        }
    }
}

What changes should I make so that the new activity starts after I click on the set date in the date picker dialog. I have tried putting the intent inside the ondatesetlistener but that doesn't seem to help.

1

There are 1 answers

2
santosh kumar On

Try this,

   import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Button;
import android.widget.ImageButton;

/**
 *To handle the main UI.
 */
public class ui extends Activity {

StringBuffer sb ;
ImageButton btn;
 int year_x,month_x,day_x;
    final int DIALOG_ID=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sb = new StringBuffer();
   btn=(ImageButton)findViewById(R.id.bread);
        btn.setOnClickListener(
                    new View.OnClickListener(){
                        @Override
                        public void onClick(View v){
                            showDialog(DIALOG_ID);

                        }
                    }
            );
    }

        protected Dialog onCreateDialog(int id){
            if(id==DIALOG_ID)
                return new DatePickerDialog(this, dPickerListener ,year_x,month_x,day_x);
            return null;
        }
        private DatePickerDialog.OnDateSetListener dPickerListener=new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                year_x=year;
                month_x=monthOfYear+1;
                day_x=dayOfMonth;
                sb.append(day_x).append(month_x).append(year_x);
                Intent j=new Intent(this,Read.class);
                            j.putExtra("date",sb.toString());
                            startActivity(j);
            }
        };

   public void onwriteclick(View v){
       if(v.getId()==R.id.bwrite){
           showDialog(DIALOG_ID);
           Intent j=new Intent(ui.this,write.class);
           j.putExtra("date",sb.toString());
           startActivity(j);
       }
   }

    public void onpdfclick(View v){
        if(v.getId()==R.id.bpdf){
            showDialog(DIALOG_ID);
            Intent j=new Intent(ui.this,noteselectorforpdf.class);
            j.putExtra("date",sb.toString());
            startActivity(j);
        }
    }
}