时间选择器绑定的Property无法保存至SQLite数据库
Let's break down the issues and fix your code step by step:
1. Fix the Property Definition (Critical First Step)
Your current IntProperty<SignInList> definition is incorrect. IntProperty (assuming you're using Codename One's Property API, given Display.getInstance()) is designed to hold integer values, not your SignInList class. This type mismatch is almost certainly causing issues when trying to read/write values to the property, and subsequently to SQLite.
Replace your property definition with one that matches the data type you actually need:
// Preferred: Store a Unix timestamp (avoids int range limits for future dates) public final LongProperty meetingTime = new LongProperty("meetingTime"); // OR if you only need an int (e.g., minutes since midnight) public final IntProperty meetingTime = new IntProperty("meetingTime");
2. Correct the Time Picker Binding
Time pickers typically return Date objects, not raw integers/longs. If you're using Codename One's PickerComponent.createTime(), the underlying picker returns a Date—so you need to convert this to a numeric value (timestamp or integer time) for your property.
Here's how to fix the binding with a converter (using LongProperty for timestamps, which is more robust):
PickerComponent meetingTimePicker = PickerComponent.createTime().label("Meeting time"); // Bind with a converter to translate between Date and timestamp (long) uib.bind(signInList.meetingTime, meetingTimePicker.getPicker(), new Converter<Long, Date>() { @Override public Date convertTo(Long timestamp) { return timestamp != null ? new Date(timestamp) : null; } @Override public Long convertFrom(Date selectedTime) { return selectedTime != null ? selectedTime.getTime() : null; } }); c.add(layout.createConstraint().widthPercentage(50), meetingTimePicker);
3. Ensure SQLite Compatibility
When saving to SQLite, make sure:
- Your database table has the correct column type: use
INTEGERfor bothintandlongvalues (SQLite handles long integers natively). - You're correctly retrieving the property's current value when inserting/updating:
Database db = Display.getInstance().openOrCreate("your_db_name.db"); // Example insert using LongProperty db.execute("INSERT INTO sign_in_list (meeting_time) VALUES (?)", signInList.meetingTime.getLong()); // Example insert using IntProperty // db.execute("INSERT INTO sign_in_list (meeting_time) VALUES (?)", // signInList.meetingTime.getInt());
4. Verify Binding Functionality
Before debugging the database, confirm your property is actually being updated:
// After selecting a time in the picker, print the property value System.out.println("Meeting Time Timestamp: " + signInList.meetingTime.getLong());
If this prints null or an unexpected value, your binding is the issue—double-check the converter logic or picker configuration.
内容的提问来源于stack exchange,提问作者Paul Wade

