Как сохранить ответ API модифицированной в области и использовать в автономном режиме на Android - PullRequest
0 голосов
/ 03 ноября 2018

Я занимаюсь разработкой приложения для Android. Я хочу сохранить ответ о модификации в Realm, и я хочу использовать его, когда мое приложение отключено и я следовал ссылка Однако, когда я запускаю приложение в автономном режиме, оно не показывает никаких данных.

ниже класса ВведениеItem.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: Введение.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;
    }

}
...