I have a problem about datepicker feature.
How to save the value of datepicker to sqlite database?
Assume I create a button to set the date and a button to save to database…
main.java
public class main extends Activity implements OnClickListener {
private Calendar dateTime=Calendar.getInstance();
private SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, yyyy");
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addshoppinglist);
mSaveBttn = (Button) findViewById(R.id.saveBttn);
mSetDateBttn = (Button) findViewById(R.id.shoppingDate);
mCancelBttn = (Button) findViewById(R.id.cancelBttn);
mSaveBttn.setOnClickListener(this);
mSetDateBttn.setOnClickListener(this);
mCancelBttn.setOnClickListener(this);
helper = new ProductDatabaseHelper(this);
model = helper.getAll();
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.saveBttn:
break;
case R.id.cancelBttn:
Intent mainIntent = (new Intent(AddShoppingList.this, main.class));
startActivity(mainIntent);
break;
case R.id.shoppingDate:
showDialog(DIALOG_DATE);
}
}
protected Dialog onCreateDialog(int id){
switch(id){
case DIALOG_DATE:
return new DatePickerDialog(this,new OnDateSetListener(){
public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth){
dateTime.set(year,monthOfYear,dayOfMonth);
mSetDateBttn.setText(dateFormatter
.format(dateTime.getTime()));
}
} ,dateTime.get(Calendar.YEAR),
dateTime.get(Calendar.MONTH),
dateTime.get(Calendar.DAY_OF_MONTH));
}
}
Does anyone know how to save the date format to database?
You can achive this in the following manner.
Now use your database helper class to insert values in it.For using database read this post.