I am trying to make the following projekt:
Start Activity has 4 buttons.
1. First Button just opens an Information Dialog (onClick0)
2. Second Button opens a DatePickerDialog and allows me to Set a Date. (onClick1)
3. Third Button opens a Dialog that shows me the date i entered (onClick2).
4. Fourth Button just opens an About Dialog. (onClick3)
The problem is that after the first Date set, the Dialog that i open pressing the Third Button show me always the First Date set i did.
What am i doing wrong?
Please help me 🙂
MainActivity code:
package boy.girl;
import java.text.DateFormat;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;
import android.content.DialogInterface;
import android.app.DatePickerDialog;
import android.app.AlertDialog;
public class MainActivity extends Activity {
DateFormat fmtDateAndTime=DateFormat.getDateTimeInstance();
Calendar dateAndTime=Calendar.getInstance();
int mYear,mMonth,mDay;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick0(View v)
{
showDialog(0);
}
public void onClick1(View v)
{
new DatePickerDialog(this, d,
dateAndTime.get(Calendar.YEAR),
dateAndTime.get(Calendar.MONTH),
dateAndTime.get(Calendar.DAY_OF_MONTH))
.show();
}
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Toast.makeText(getBaseContext(), "Set", Toast.LENGTH_SHORT).show();
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
dateAndTime.set(Calendar.YEAR, year);
dateAndTime.set(Calendar.MONTH, monthOfYear);
dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
Toast.makeText(getBaseContext(),
Integer.toString(mDay)+" "+Integer.toString(mMonth)+" "+Integer.toString(mYear),
Toast.LENGTH_SHORT).show();
}
};
public void onClick2(View v)
{
showDialog(2);
}
public void onClick3(View v) { showDialog(3); }
@Override
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case 0:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.infobox);
builder.setTitle("Title Information");
builder.setMessage("Information text...");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Ok clicked", Toast.LENGTH_SHORT)
.show();
}
});
return builder.create();
case 2:
return new AlertDialog.Builder(this)
.setTitle("Results")
.setMessage(Integer.toString(mDay)+" "+Integer.toString(mMonth)+
" "+Integer.toString(mYear))
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Ok clicked", Toast.LENGTH_SHORT)
.show();
}
}
).create();
case 3:
return new AlertDialog.Builder(this)
.setTitle("About")
.setMessage("About this company and App version")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Ok clicked", Toast.LENGTH_SHORT)
.show();
}
}
).create();
}
return null;
}
}
onCreateDialogis called only once. Therefore it is always showing the data from the first time you opened it.You need to update the message in
onPrepareDialog. That should get it working for you.PS: onCreateDialog and onPrepareDialog are deprecated in ICS, and also a little buggy in ICS. Try using the new APIs if possible.
Edit:
Keep your code as it is, just add the following –