领域查询结果始终为null

时间:2018-07-23 12:03:03

标签: java android realm

我有一个包含某些Item的Realm数据库。我已经填充了从在线数据库获取信息的数据库,但是当我尝试在数据库上执行查询时,它始终提供空数据。

确保来自数据库的数据不为空。

我已经按照官方网站显示的内容填充了数据库。

我在这里使用查询结果:

public void onBindViewHolder(ViewHolder holder, int position) {

    Item result = ItemHandler.getItem(mDataset.get(position));

    Picasso.get().load(result.getImg()).resize(100,100).onlyScaleDown().centerCrop().into(holder.itemImage);
    holder.itemName.setText(result.getName());
    holder.itemValue.setText(result.getSell_value());

    //TODO when clicked open a dialog

}

查询方法:

public static Item getItem(int id){

    Realm realm = Realm.getDefaultInstance();
    return realm.where(Item.class).equalTo("id", id).findFirst();

}

物品类别:

public class Item extends RealmObject {

private int id;
private String name;
private String description;
private int img;
private int buff_health;
private int buff_damage;
private int buff_armor;
private int buy_value;
private int sell_value;

public int getBuy_value() {
    return buy_value;
}

public void setBuy_value(int buy_value) {
    this.buy_value = buy_value;
}

public int getSell_value() {
    return sell_value;
}

public void setSell_value(int sell_value) {
    this.sell_value = sell_value;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getImg() {
    return img;
}

public void setImg(int img) {
    this.img = img;
}

public int getBuff_health() {
    return buff_health;
}

public void setBuff_health(int buff_health) {
    this.buff_health = buff_health;
}

public int getBuff_damage() {
    return buff_damage;
}

public void setBuff_damage(int buff_damage) {
    this.buff_damage = buff_damage;
}

public int getBuff_armor() {
    return buff_armor;
}

public void setBuff_armor(int buff_armor) {
    this.buff_armor = buff_armor;
}
}

编辑:这是我存储数据的地方:

public static void addToDB(List<ItemMaster> itemMasterList, Resources resource){

    Realm myRealm = Realm.getDefaultInstance();

    for(int i = 0; i < itemMasterList.size(); i++){

        myRealm.beginTransaction();

        Item item = myRealm.createObject(Item.class);
        item.setId(itemMasterList.get(i).getId());
        item.setName(itemMasterList.get(i).getName());
        item.setDescription(itemMasterList.get(i).getDescription());
        item.setBuff_armor(itemMasterList.get(i).getBuff_armor());
        item.setBuff_damage(itemMasterList.get(i).getBuff_damage());
        item.setBuff_health(itemMasterList.get(i).getBuff_health());
        item.setBuy_value(itemMasterList.get(i).getBuy_value());
        item.setSell_value(itemMasterList.get(i).getSell_value());
        item.setImg(resource.getIdentifier("item_" + itemMasterList.get(i).getId(), "drawable", null));

        myRealm.commitTransaction();

    }

    myRealm.close();

    //TODO check this, last watch, not saving

}

0 个答案:

没有答案