You need to enable JavaScript to run this app. 时间选择器绑定的Property无法保存至SQLite数据库
优惠活动
大模型
产品
解决方案
定价
更多

时间选择器绑定的Property无法保存至SQLite数据库

Troubleshooting SQLite Save Failure for Your Time Picker Property

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 INTEGER for both int and long values (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:17:36