Как правильно реализовать царство в оффлайн андроиде? - PullRequest
0 голосов
/ 02 ноября 2018

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

Ниже приведен мой ВведениеItem.java, где я сохранил ответ на Retrofit для RealmDatabase:

public class IntroductionItem extends AppCompatActivity {

    public RealmList<Introduction> introductionList;

    public IntroductionAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.introduction);
        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();
                Realm.init(IntroductionItem.this);

                RealmConfiguration config = new RealmConfiguration.Builder()
                        .name("overview.realm")
                        .schemaVersion(1)
                        .deleteRealmIfMigrationNeeded()
                        .build();
                Realm.setDefaultConfiguration(config);


                // add response to realm database
                Realm realm = Realm.getInstance(config);
                realm.beginTransaction();
                realm.copyToRealmOrUpdate(introductionList);
                realm.commitTransaction();



// programmatically check : data is inserted in to realm or not

                int notesCount = realm.where(KitabSawti.class).findAll().size();
                int notesCount2 = realm.where(Introduction.class).findAll().size();


                Log.d("my first", String.valueOf(notesCount));
                Log.d("my second", String.valueOf(notesCount2));


                RecyclerView recyclerView = findViewById(R.id.recyclerView);
                recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                adapter = new IntroductionAdapter(IntroductionItem.this, introductionList); // changes
                recyclerView.setAdapter(adapter);
                realm.close();
            }

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

            }
        });
    }
}

под классом KitabSawti.java 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;
    }

}

below Introduction.java class
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;
    }

}
...