我正在开发一个android应用程序,其中在脱机测试时保存了对领域数据库的改造响应,但它无法正常工作,如何正确地逐步实现领域,以便我的应用程序可以在脱机模式下工作。
下面是我的IntroductionItem.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;
}
}