【问题标题】:SugarORM - 'listall' returns empty listSugarORM - 'listall' 返回空列表
【发布时间】:2023-03-14 18:00:01
【问题描述】:

我有这门课:

public class ProfileData extends SugarRecord {
@Expose
public Data data;

public ProfileData(Data data) {
    this.data = data;
}

public static class Data {
    public long id;
    @Expose
    public String email;
    @Expose
    public String name;
    @Expose
    public String surname;
    @Expose
    public String address;
    @Expose
    public String city;
    @Expose
    public long pesel;
    @Expose
    public long phone;
    @Expose
    public long userId;
    @Expose
    public long clubId;
    @Expose
    public String fullName;
    @Expose
    public long clientStatusId;
    @Expose
    public String provider;
    @Expose
    public String uid;
    @Expose
    public boolean gender;


    public Data(long id, String email, String name, String surname, String address, String city,
                long pesel, long phone, long userId, long clubId, String fullName,
                long clientStatusId, String provider, String uid, boolean gender) {
        this.id = id;
        this.email = email;
        this.name = name;
        this.surname = surname;
        this.address = address;
        this.city = city;
        this.pesel = pesel;
        this.phone = phone;
        this.userId = userId;
        this.clubId = clubId;
        this.fullName = fullName;
        this.clientStatusId = clientStatusId;
        this.provider = provider;
        this.uid = uid;
        this.gender = gender;
    }
}

我用来自 JSON 的数据(使用 Gson)填充这个类。 然后我调用 save() 方法('data' 对象是 ProfileData 的一个实例):

data.save();

我的下一步是:

List<ProfileData> profileList = ProfileData.listAll(ProfileData.class);

而 profileList 是空的。

我把这行放到 AndroidManifest 中:

<meta-data
        android:name="DATABASE"
        android:value="sugar_example_my.db" />
    <meta-data
        android:name="VERSION"
        android:value="2" />
    <meta-data
        android:name="QUERY_LOG"
        android:value="true" />
    <meta-data
        android:name="DOMAIN_PACKAGE_NAME"
        android:value="com.example.my" />

最后在我的 App 类中,我输入了这一行:

SugarContext.init(this);

有什么建议吗?

编辑:

我解决了这个问题,将 public Data() {} 添加到 Data 类中并移动

扩展了 SugarRecord

从 ProfileData 到 Data。

【问题讨论】:

    标签: android sugarorm


    【解决方案1】:

    我认为问题在于你的列表应该是List&lt;Data&gt;

    【讨论】:

    • 是的,这个。我还向 ProfileData 和 Data 添加了默认构造函数。现在它运行良好。
    • 拜托,这个 ORM 比默认光标操作更快吗?
    猜你喜欢
    • 2015-09-04
    • 2019-08-18
    • 2021-03-11
    • 2015-10-27
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多