In my application i have 3 text view arranged horizontally,1st previmage,2nd date,3rd nextimage.Now,when i click the 1st textview i should should display the previous month,(its printing) when once again i click means it should go to 2nd previous month of current month and this should go on..But when i click 2nd time it is coming the 1st previous month only its doesn’t going backwards..Please help me.Thanks in advance..
This is my code:
prvmon.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
System.out.println("Inside left");
TextView date=(TextView)findViewById(R.id.date);
Calendar cal= Calendar.getInstance();
cal.add(cal.MONTH,-1);
Date date1=cal.getTime();
System.out.println("sssssss="+date1);
cal.setTime(date1);
SimpleDateFormat month_date = new SimpleDateFormat("MMMMM yyyy");
String month_name = month_date.format(cal.getTime());
date.setText(month_name);
}
});
The reason is that you are initializing your Calender instance everytime. So, its taking the current date/time instance. So, better would be initialize in once and use it again. Declare it globally and initialize it in
onCreate().now, use this calender instance in your
onClick()event.