naveen
naveen

Reputation: 19

onDateChanged in onclick fires after two clicks that is when i click third time on ok button

Hi everyone I am getting an error that onClick fires after i click two times, i want it to be performed on first click . Here in this code i took one date-picker to display date, on user selecting one date and press ok button that date must be displayed in TextView.

this is My Code

public class TimePicker extends Activity{
protected static OnDateChangedListener OnDateChangedListener;
private int year;
private int month;
private int day;
private String Date;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.time_picker);   
    Calendar cal = Calendar.getInstance();
    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH);
    day = cal.get(Calendar.DAY_OF_MONTH);
    final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);      


    Button b = (Button) findViewById(R.id.btnOk);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub  
            OnDateChangedListener = new OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker v, int selectedYear, int selectedMonth, int selectedDay) {
                    // TODO Auto-generated method stub
                    year = selectedYear;
                    month = selectedMonth;
                    day = selectedDay;
                }
            };  
            dp.init(year, month, day, OnDateChangedListener);
            StringBuilder sb = new StringBuilder();
            sb.append(day).append("-").append(month+1).append("-").append(year);
            Date = sb.toString();
            TextView txt = (TextView) findViewById(R.id.textViewTime);
            txt.setText(Date);  
        }
    }); 

} }

Upvotes: 0

Views: 127

Answers (1)

Kyle Emmanuel
Kyle Emmanuel

Reputation: 2221

Move this line dp.init(year, month, day, OnDateChangedListener); below the OnDateChangedListener instantiation. it gets invoked the 2nd time because the 1st time it was just initiated.

You should also make txt into a field and call setText within OnDateChanged after assigning Date its new value. remove the call to setText within onClick.

i.e.

OnDateChangedListener = new OnDateChangedListener() {
     @Override
     public void onDateChanged(DatePicker v, int selectedYear, int selectedMonth, int selectedDay) {
       // TODO Auto-generated method stub
       year = selectedYear;
       month = selectedMonth;
       day = selectedDay;
       displayDate();
    }
};

dp.init(year, month, day, OnDateChangedListener);

displayDate();

You can create a method for populating the txt field:

private void displayDate() {
   StringBuilder sb = new StringBuilder();
            sb.append(day).append("-").append(month+1).append("-").append(year);
   Date = sb.toString();
   TextView txt = (TextView) findViewById(R.id.textViewTime);
   txt.setText(Date);  
}

Upvotes: 1

Related Questions