我正在开发一个Android Activity,它读取用户输入的出生日期,并使用该值实例化一个对象。然后将日期存储在SQLite表中。问题是,当我稍后在查询表时,我得到一个NULL日期值。
对象的构造函数正确接收日期。但是,稍后查询时它会返回NULL。
如果你能指出错误,那将会很棒。
读取Date的方法,实例化对象并存储在SQLite中。正在从EditText dob
中读取该值 b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final EditText name = (EditText) findViewById(R.id.nameinput);
final EditText dob = (EditText) findViewById(R.id.dobinput);
final EditText mobile_no = (EditText) findViewById(R.id.mnuminput);
final EditText alt_no = (EditText) findViewById(R.id.altnuminput);
final EditText email_id = (EditText) findViewById(R.id.emailinput);
final EditText home_adr = (EditText) findViewById(R.id.homeadrinput);
final EditText off_adr = (EditText) findViewById(R.id.offadrinput);
final EditText notes = (EditText) findViewById(R.id.notesinput);
final TextView flag = (TextView) findViewById(R.id.insertflaglabel);
String name_converted = name.getText().toString();
String mobile_no_converted = mobile_no.getText().toString();
String alt_no_converted = alt_no.getText().toString();
String email_id_converted = email_id.getText().toString();
String home_adr_converted = home_adr.getText().toString();
String off_adr_converted = off_adr.getText().toString();
String notes_converted = notes.getText().toString();
DateFormat df = DateFormat.getDateInstance();
Date dob_converted = new Date(DateFormat.MEDIUM);
try {
dob_converted = df.parse(dob.getText().toString());
} catch (ParseException e) {
e.printStackTrace();
}
Log.d("add_client", "Date read is " +dob_converted);
client client_new = new client(name_converted, dob_converted, mobile_no_converted, alt_no_converted, home_adr_converted, off_adr_converted, email_id_converted, notes_converted);
Context context = getBaseContext();
database_handler insert_client = new database_handler(context);
Log.d("add_client", "Date instantiated is " +client_new.getDOB());
insert_client.addClient(client_new);
flag.setText("Client Details Saved!");
}
});
}
使用.getDOB()查询SQLite的出生日期的方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_clientdetails);
Bundle extras = getIntent().getExtras();
Integer tab_id = 0;
if (extras != null) {
tab_id = (int) extras.getInt("client_id");
}
TextView namev = (TextView) findViewById(R.id.namevalue);
TextView mobnum = (TextView) findViewById(R.id.mnumvalue);
TextView date_ob = (TextView) findViewById(R.id.dobvalue);
TextView altnum = (TextView) findViewById(R.id.altnumvalue);
TextView off_addr = (TextView) findViewById(R.id.offadrvalue);
TextView home_addr = (TextView) findViewById(R.id.homeadrvalue);
TextView email_addr = (TextView) findViewById(R.id.emailvalue);
TextView notes_client = (TextView) findViewById(R.id.notesvalue);
Context c_displayclient = getBaseContext();
database_handler dhandle = new database_handler(c_displayclient);
String TAG = "display_clientdetails";
Log.d(TAG, "Value of ID is " +tab_id);
final client c_details = dhandle.getClient(tab_id);
namev.setText(c_details.getName());
mobnum.setText(c_details.getMobile_number());
altnum.setText(c_details.getAlt_num());
DateFormat datef = DateFormat.getDateInstance(DateFormat.MEDIUM);
Log.d("display_clientdetails", "Date of Birth is " +c_details.getDOB());
String text_date = datef.format(c_details.getDOB());
date_ob.setText(text_date);
实例化代码
// constructor
public client(String name,Date dob,String mobile_number,String alternate_number,String office_address, String home_address,String email_id,String notes)
{
this.name = name;
this.dob=dob;
Log.d("client", "dob received in client constructor is " +this.dob);
this.mobile_number = mobile_number;
this.alternate_number=alternate_number;
this.office_address=office_address;
this.home_address=home_address;
this.email_id=email_id;
this.notes=notes;
}
返回出生日期的方法
// getting Date of Birth
public Date getDOB(){
Log.d("client", "Date returned is " +this.dob);
return this.dob;
}
向数据库表添加新条目的方法
public void addClient(client client_instance) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, client_instance.getName()); // Client Name
values.put(KEY_MO_NO, client_instance.getMobile_number()); // Client Phone
values.put(KEY_DOB, client_instance.getDOB().toString());
values.put(KEY_ALT_NO, client_instance.getAlt_num());
values.put(KEY_OFF_ADR, client_instance.getOff_adr());
values.put(KEY_HOM_ADR, client_instance.getHome_adr());
values.put(KEY_EMAIL, client_instance.getEmail());
values.put(KEY_NOTES, client_instance.getNotes());
db.insert(TABLE_CLIENTS, null, values);
db.close(); // Closing database connection
}
从数据库获取数据的方法
// Getting single contact
client getClient(int id) {
SQLiteDatabase db = this.getReadableDatabase();
client client_instance = new client();
String TAG ="database_handler";
Cursor cursor = db.query(TABLE_CLIENTS, new String[] {
KEY_NAME, KEY_MO_NO, KEY_DOB, KEY_ALT_NO, KEY_OFF_ADR, KEY_HOM_ADR, KEY_EMAIL, KEY_NOTES}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if(cursor != null) Log.d(TAG, "cursor is not null");
// if(cursor.moveToFirst()) Log.d(TAG, "move to first is true");
if (cursor != null && cursor.moveToFirst()) {
client_instance.setName(cursor.getString(0));
client_instance.setMobileNumber(cursor.getString(1));
//Convert Datefield from String to Date so that the setDOB method can accept it as argument
DateFormat dateform = DateFormat.getDateInstance(DateFormat.FULL);
Date d_of_b = new Date(DateFormat.FULL);
try {
d_of_b = dateform.parse(cursor.getString(2));
} catch (ParseException e) {
e.printStackTrace();
}
//Set Date of Birth
client_instance.setDOB(d_of_b);
client_instance.setAlt_num(cursor.getString(3));
client_instance.setOff_adr(cursor.getString(4));
client_instance.setHome_adr(cursor.getString(5));
client_instance.setEmail(cursor.getString(6));
client_instance.setNotes(cursor.getString(7));
Log.d(TAG, "Email is " +cursor.getString(3));
cursor.close();
}
return client_instance;
}
将出生日期设置为新客户端实例的方法
public void setDOB(Date dob){
this.dob = dob;
}
答案 0 :(得分:0)
您的问题是格式化时必须使用相同的日期格式。解析日期。
您应该使用特定日期格式,例如" MM / dd / yyyy"
您的解决方案是
格式化日期(将客户端插入SQLite时)
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String dobAsString = format.format ( dob_Date_object);
// Store dobAsString in SQLite
解析日期时(从SQLite读取客户端时)
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String dobAsString = // ... Read from SQLite
Date dob = format.parse(dobAsString);
您不应将String用于商店DATE数据。您应该使用Long数据类型(将日期转换为时间戳 - 长数据类型)。见以下原因: