从realm文件加载数据

时间:2016-01-24 03:51:43

标签: java android realm

我喜欢使用getter加载我的json中的整数,但它不起作用。我做错了什么

 int position;

 Setting setting = new Setting();
 position = setting.getRadioPosition(); 

我的领域课程如下:

public class Setting extends RealmObject {
  @Required
  private Integer RadioPosition;

  public Integer getRadioPosition() {
    return RadioPosition;
  }

  public void setRadioPosition(Integer radioposition) {
    RadioPosition = radioposition;
  }

}

我的Json文件如下:

[
  {
"Notification": "True",
"RadioPosition": 1
   }
 ]

1 个答案:

答案 0 :(得分:0)

我应该先添加以下内容来加载所有数据。

RealmResults test = realm.where(Setting.class).findAll();

以下是我插入两个json文件的方法:

 private void loadJsonFromStream() throws IOException {

    InputStream stream = getAssets().open("school.json");
    InputStream streamSetting = getAssets().open("setting.json");

    realm.beginTransaction();
    try {
        realm.createAllFromJson(School.class, stream);
        realm.createAllFromJson(Setting.class, streamSetting);
        realm.commitTransaction();
    } catch (IOException e) {
        // Remember to cancel the transaction if anything goes wrong.
        realm.cancelTransaction();
    } finally {
        if (stream != null && streamSetting != null) {
            stream.close();
            streamSetting.close();


        }
    }

}