如何将改造API响应保存到领域并在Android上离线使用

时间:2018-11-03 10:31:38

标签: java android realm retrofit offline

我正在开发android应用,我想将改造响应保存到Realm中,并且我想在我的应用离线时使用它,并且我已经关注了 link但是,当我以离线模式运行应用程序时,它没有显示任何数据。

  

在我实现了领域的IntroductionItem.java类下面

public class IntroductionItem extends AppCompatActivity {

    public RealmList<Introduction> introductionList;


   public Context context;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.introduction);


        Realm.init(IntroductionItem.this);
      RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();

      final Realm realm = Realm.getInstance(realmConfiguration);

        KitabInterface kitabInterface = ApiClient.getApiService();


        Call<KitabSawti> call = kitabInterface.getIntroduction();


        call.enqueue(new Callback<KitabSawti>() {
            @Override
            public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {

                introductionList = response.body().getIntroduction();
                RecyclerView recyclerView = findViewById(R.id.recyclerView);
                recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                recyclerView.setAdapter( new IntroductionAdapter(IntroductionItem.this, introductionList));
                for(int i = 0; i < introductionList.size(); i++)
                     realm.beginTransaction();
                 Introduction  introduction= realm.createObject(Introduction.class);
                  introduction.setImage(introductionList.get(0).getImage());
                  introduction.setIntroduction(introductionList.get(0).getIntroduction());
                  realm.commitTransaction();
                  recyclerView.setVisibility(View.VISIBLE);

                // changes

               // realm.close();
            }


            @Override
            public void onFailure(Call<KitabSawti> call, Throwable t) {

            }
        });
    }
}

Pojo:Introduction.java

public class Introduction  extends RealmObject {

    @SerializedName("image")
    @Expose
    private String image;
    @SerializedName("introduction")
    @Expose
    private String introduction;

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

}

在KitabSawti Pojo课下

public class KitabSawti extends RealmObject {

    @SerializedName("Introduction")
    @Expose
    private RealmList<Introduction> introduction = null;
    @SerializedName("Education")
    @Expose
    private RealmList<Education> education = null;
    @SerializedName("Work")
    @Expose
    private RealmList<Work> work = null;
    @SerializedName("Skills")
    @Expose
    private RealmList<Skill> skills = null;
    @SerializedName("Contact")
    @Expose
    private RealmList<Contact> contact = null;

    public RealmList<Introduction> getIntroduction() {
        return introduction;
    }

    public void setIntroduction(RealmList<Introduction> introduction) {
        this.introduction = introduction;
    }

    public RealmList<Education> getEducation() {
        return education;
    }

    public void setEducation(RealmList<Education> education) {
        this.education = education;
    }

    public RealmList<Work> getWork() {
        return (RealmList<Work>) work;
    }

    public void setWork(RealmList<Work> work) {
        this.work = work;
    }

    public RealmList<Skill> getSkills() {
        return (RealmList<Skill>) skills;
    }

    public void setSkills(RealmList<Skill> skills) {
        this.skills = skills;
    }

    public RealmList<Contact> getContact() {
        return (RealmList<Contact>) contact;
    }

    public void setContact(RealmList<Contact> contact) {
        this.contact = contact;
    }

}

0 个答案:

没有答案