In my App I am using Realm as local database. I am using recyclerview widget to show these data. Now initiallay I want to show some preload data in recycler view which would be stored in realm as well. then I will implement add, edit, delete method. But I am having fatal error while trying to run this app. I am very new in Realm. I cannot identify which problem is this.
I have Solved this problem with the help of answer code. here is the solution for this.
Solved Code
And My Activity Class is
public class MyColleaguesPage extends AppCompatActivity {
private RecyclerView recyclerView;
private MyColleaguesAdapter adapter;
private Realm colleagueRealm;
private List<MyColleagueModel> colleagueObject;
private RealmResults<MyColleagueModel> dataResult;
private static final String DIALOG_TAG = "EmployeeDialog";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
colleagueRealm = Realm.getDefaultInstance();
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
setUpRecycler();
if (!Prefs.with(this).getPreLoad()) {
setRealmData();
}
showAllPersons();
}
private void showAllPersons() {
dataResult = colleagueRealm.where(MyColleagueModel.class).findAll();
setAdapter(dataResult);
adapter.notifyDataSetChanged();
}
//find all objects in the Book.class
private void setAdapter(RealmResults<MyColleagueModel> results) {
adapter = new MyColleaguesAdapter(this, results);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void setUpRecycler() {
recyclerView.setHasFixedSize(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
}
private void setRealmData(){
List<MyColleagueModel> colleague = new ArrayList<>();
MyColleagueModel model = new MyColleagueModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
colleague.add(model);
model = new MyColleagueModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Comapny2");
model.setTitle("Title1");
colleague.add(model);
model = new MyColleagueModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Comapny3");
model.setTitle("Title3");
colleague.add(model);
for (MyColleagueModel realmModel : colleague) {
// Persist the colleague data
colleagueRealm.beginTransaction();
colleagueRealm.copyToRealm(realmModel);
colleagueRealm.commitTransaction();
}
Prefs.with(this).setPreLoad(true);
}
.....
To fix immediate problem
should be
But based on
Prefs.with(this).preload()
you are basing your code on Ravi Tamada's tutorial which is full of terrible practices and I think it's still Realm 0.82.2.Don't forget that latest version of Realm (at time of writing) is Realm 3.5.0, and you are better off with the official documentation.
For example, you should use
RealmRecyclerViewAdapter
provided byrealm-android-adapters
(official add-on) instead of using your own.EDIT: Here is full solution
set unique value to
model.setId();
insetRealmData()
method, it will work,You are trying set the primary key value which alreay exits.
Create auto increment in realm ref this SO answer