在Android中过滤Realm对象 [英] Filtering Realm object in android

查看:158
本文介绍了在Android中过滤Realm对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我开始使用Realm,一切正常,几乎所有东西都正常。

So i've started to use Realm and everything works fine, well almost everything.

我正在使用MultiAutoCompleteTextView选择一些用户(RealmObject)

I'm using a MultiAutoCompleteTextView to select some user (RealmObject)

所以去了:

这是我的Filter(适配器的内部类)

this is my Filter( inner class of my adapter)

private class UserFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();
        if (constraint == null || constraint.length() == 0) {
            filterResults.values = mUsers;
            filterResults.count = mUsers.size();
        } else {
            final String lastToken = constraint.toString().toLowerCase();
            final List<User> list = new ArrayList<>();

            RealmQuery<User> query = realm.where(User.class);
            query.contains("nickname", lastToken, false);
            RealmResults<User> result = query.findAll();
            list.addAll(result);


            filterResults.values = list;
            filterResults.count = list.size();
        }
        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mFilteredUsers = (List<User>) results.values;
        if (results.count > 0) {
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }
    }

在我的适配器中:
在getView方法:

And in my adapter: in the getView method:

EGCUser user = getItem(position);

holder.mName.setText(user.getNickname());

用户是无效的对象,我尝试了很多不同的事情,但一切都失败了。
所以我想知道我能做什么呢?
我有很多线程问题,所以也许是内部类有问题?

user is an invalid object, i've been trying so many different thing and everything failed. So I'm wondering what can I do the achieved that. I've got a lot of thread issue so maybe it's a problem with the inner class ?

谢谢

编辑:在这种情况下,我应该在哪里做Realm.getInstance()?

in this situation, where am I supposed to do the Realm.getInstance()?

现在,我在适配器中传递了一个上下文,我在适配器的构造函数中执行此操作,并且将领域对象存储在变量中。

Right now, I pass a context in my adapter and i'm doing it in the constructor of the adapter, and i'm stocking the realm object in a variable.

EDIT2:我可以使用它,但是我没有知道这是否应该做:

I got it to work but I don't know if this is how we're supposed to do:

在我的performFiltering中,我这样做:

In my performFiltering I did this:

((Activity)mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    final List<User> list = new ArrayList<>();
                    Realm realm = Realm.getInstance(mContext);
                    RealmQuery<User> query = realm.where(User.class);
                    query.contains("nickname", lastToken, false);
                    RealmResults<User> result1 = query.findAll();
                    list.addAll(result1);

                    filterResults.values = list;
                    filterResults.count = list.size();

                }
            });

但是我会公开征求反馈。

But i'll this open for feedback.

推荐答案

来自Realm的基督徒。不幸的是,由于我们的线程限制,Realm当前不支持Filter类(而Filter在后台线程上工作)。但是,我们在TODO上有它,您可以在这里关注进度: https:// github.com/realm/realm-android-adapters/issues/79

Christian from Realm here. Unfortunately Realm currently doesn't support the Filter class due to our Thread restrictions (and Filter does it's job on a background thread). We have it on our TODO however and you can follow progress here: https://github.com/realm/realm-android-adapters/issues/79

直到您有两个选择:

1)执行筛选UI线程。如果您在Realm中没有太多项目,或者查询相对简单,则可能会发现它足够快。我们已经有了使用Realm的键盘应用程序。

1) Perform the filtering the the UI thread. If you don't have that many items in Realm or the query is relative simple, you will probably find that this is fast enough. We already have keyboard apps using Realm that does it this way.

2)除了返回适当的Realm对象之外,您还需要读取任何需要显示的数据并从<而是使用code> performFiltering()方法。

2) Instead of returning proper Realm objects, read whatever data you need to display and return that from the performFiltering() method instead.

这篇关于在Android中过滤Realm对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆