'select from table with title in Realm
I'm beginner in Real programming.I used Sqlite before and now i try to learn Realm.I found one tutorial and everything is perfect
I can show all my values in RecyclerView.But i have one question.How i can return all values from database where title equals for example hello and got i can show it in RecyclerView This is java code
@Bind(R.id.edit_title) EditText mEditTitle;
@Bind(R.id.recycler_view)
RecyclerView mRecyclerView;
private Realm mRealm;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRealm = Realm.getInstance(getContext());
}
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edition, container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRealm.beginTransaction();
RealmResults<MyBook> books = mRealm.where(MyBook.class).equalTo("title", "Hello").findAll();
mRealm.commitTransaction();
mRecyclerView.setAdapter(new MyListAdapter(mRealm.allObjects(MyBook.class)));
}
@Override
public void onDestroy() {
super.onDestroy();
mRealm.close();
}
@OnClick(R.id.button_add)
public void onAddClick() {
mRealm.beginTransaction();
MyBook book = mRealm.createObject(MyBook.class);
book.setTitle(getTrimmedTitle());
mRealm.commitTransaction();
}
@OnClick(R.id.button_remove)
public void onRemoveClick() {
mRealm.beginTransaction();
RealmResults<MyBook> books = mRealm.where(MyBook.class).equalTo("title", getTrimmedTitle()).findAll();
if(!books.isEmpty()) {
for(int i = books.size() - 1; i >= 0; i--) {
books.get(i).removeFromRealm();
}
}
mRealm.commitTransaction();
}
private String getTrimmedTitle() {
return mEditTitle.getText().toString().trim();
}
if anyone knows solution please help me. P.s as i said i'm beginner Realm programming and good tutorials or examples would be perfect for me thanks everyone
Solution 1:[1]
Realm works like any other database, make a query. This is for a Iterator, but you can have a List
Realm realm = Realm.getDefaultInstance();
Iterator all = (MyBook)realm.where(MyBook.class).equalTo("title", "hello").findAll().iterator ();
Where title is the name of your variable in MyBook class and hello is the name you are looking for
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dinorah Tovar |
