尋找解決方案3天后我希望有人可以幫助我在這裏,我是一個新的android開發,我試圖保存datepicker對話框或我的datepicker部件的日期爲我的SQLite數據庫,我沒有得到任何錯誤,但它救了我的日期,1969年12月31日,無論我做什麼,這是我的DatePickerFragment類保存約會日期到數據庫
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private Calendar dateTime = Calendar.getInstance();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Using current date as start Date
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Get DatePicker Dialog
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
TextView chooseDateLabel = (TextView)getActivity().findViewById(R.id.chooseDateLabel);
dateTime.set(year, monthOfYear, dayOfMonth);
final int myYear = year;
final int myMonth = monthOfYear;
final int myDay = dayOfMonth;
chooseDateLabel.setText(new StringBuilder().append(myMonth + 1).append("/").append(myDay)
.append("/").append(myYear).append(" "));
DatePicker datePicker = (DatePicker)getActivity().findViewById(R.id.saveJobDatePicker);
datePicker.init(myYear, myMonth, myDay, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year = myYear;
monthOfYear = myMonth;
dayOfMonth = myDay;
ContentValues values = new ContentValues();
values.put("productDate", year + monthOfYear + dayOfMonth);
}
});
ContentValues values = new ContentValues();
values.put("date", myDay + myMonth + myYear);
}
}
,你可以看到我有沒有顯示已選定的日期一個TextView問題。 這是我添加到表的DataBaseHelper代碼。
//add content to table
public void addProduct(MyProduct product){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PRODUCT_NAME, product.getproductName());
values.put(PRODUCT_NUMBER, product.getproductNumber());
values.put(PRODUCT_WEIGHT, product.getproductWeight());
values.put(PRODUCT_QTY, product.getproductQTY());
values.put(DATE_NAME, product.getproductDate());
db.insert(TABLE_NAME, null, values);
Log.v("Wish successfully!", "yeah!!");
db.close();
}
這就是我試圖保存日期的方法。是這樣
@Override
public void onCreate(SQLiteDatabase db) {
//create our table
db.execSQL("CREATE TABLE " + TABLE_NAME+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT, PRODUCTNAME TEXT, PRODUCTNUMBER INTEGER, PRODUCTQTY REAL, PRODUCTWEIGHT REAL, PRODUCTDATE LONG, PRODUCTCOMPLETED INTEGER)");
}
我getView方法中我'嘗試我的長格式化字符串等產生
private void saveProductDB() {
Intent intent = getIntent();
final Double myLabelResults = intent.getDoubleExtra("totalQTY", 0.0);
final MyProduct product = new MyProduct();
product.setproductName(saveproductNameText.getText().toString().trim());
product.setproductNumber(Integer.valueOf(saveproductNumberText.getText().toString().trim()));
product.setproductWeight(myLabelResults);
product.setproductQTY(myLabelResults/14);
product.getproductPavingOnLabel();
saveProductDate = (DatePicker)findViewById(R.id.saveProductDatePicker);
Calendar calendar = Calendar.getInstance();
int day = saveProductDate.getDayOfMonth();
int month = saveProductDate.getMonth();
int year = saveProductDate.getYear();
calendar.set(day, month, year);
long myLongDate = calendar.getTimeInMillis();
product.setProductDate(myLongDate);
dba.Product(product);
dba.close();
saveproductNameText.setText("");
saveproductNumberText.setText("");
Intent i = new Intent(SaveProduct.this, MyProductActivity.class);
startActivity(i);
//Toast.makeText(getApplicationContext(),"Product Saved", Toast.LENGTH_LONG).show();
}
我的數據庫表這個
long dateFromProductDate = holder.myProduct.getProductDate();
String dateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(dateFromProductDate));
holder.mJobsDate.setText(dateString);
有人可以幫我找出我在哪裏犯錯,謝謝。
「無論我做什麼,它都將我的日期保存爲12/31/1969」 - 您想如何保存日期? – Inducesmile
對不起,我的意思是,這是不是從datepicker保存日期和其默認爲12/31/1969,並忽略日期選擇器上的選定日期 – Vacano