我是Real编程的初学者。之前我使用过Sqlite,现在我尝试学习Realm.I找到了一个教程,一切都很完美
https://docs.oracle.com/cd/B19306_01/server.102/b14223/aggreg.htm
我可以在RecyclerView中显示我的所有值。但我有一个问题。如何从数据库中返回所有值,其中title等于例如hello并得到我可以在RecyclerView中显示它 这是java代码
@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();
}
如果有人知道解决方案,请帮助我。 P.s我说我是初学者Realm编程和好的教程或例子对我来说是完美的 谢谢大家
答案 0 :(得分:0)
Realm与任何其他数据库一样,进行查询。 这适用于Iterator,但您可以拥有List
Realm realm = Realm.getDefaultInstance();
Iterator all = (MyBook)realm.where(MyBook.class).equalTo("title", "hello").findAll().iterator ();
其中title是MyBook类中变量的名称,hello是您要查找的名称