在RealmRecyclerViewAdapter上调用updateData实际上并未更新数据

时间:2017-11-21 00:14:24

标签: java android realm

我正在尝试使用SearchView过滤RealmRecyclerViewAdapter提供的列表。我在StackOverflow上尝试了几个建议,包括: EpicPandaForce's suggestion

无济于事。当我到达这里:updateData(新领域查询)时,没有任何反应。

这是我的代码。我从Lucas Urbas的材料大师/细节案例研究开始,并开始添加内容以帮助我自己学习如何。在PeopleFragment中,我修改了setUpToolbar和setUpRecyclerView方法,如下所示:

private void setUpToolbar() {
    if (DEBUG) Log.i(TAG, "Reached setUpToolbar") ;
    CustomAppBar appBar = ((MainActivity) getActivity()).getCustomAppBar() ;
    appBar.setTitle(getString(R.string.fragment_people__title)) ;
    appBar.setMenuRes(R.menu.people_general, R.menu.people_specific, R.menu.people_merged) ;

    // Set up search
    searchItem = appBar.getToolbarSpecific().getMenu().findItem(R.id.menu_people__search) ;
    searchView = (SearchView) searchItem.getActionView() ;
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if(DEBUG) Log.i(TAG, "Reached onQueryTextSubmit") ;
            adapter.updateData(realm.where(Person.class).equalTo("userId", userid).contains("name", query).findAll()) ;
            return true ;
            //return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if(DEBUG) Log.i(TAG, "Reached onQueryTextChange") ;
            adapter.updateData(realm.where(Person.class).equalTo("userId", userid).contains("name", newText).findAll()) ;
            return true ;
            //return false;
        }
    }) ;

    // Set up menu item click listener
    appBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (DEBUG) Log.i(TAG, "Reached onMenuItemClick") ;
            // handle selection of action bar items
            switch (item.getItemId()) {
                case R.id.menu_people__add:
                    presenter.clickAddPerson() ;
                    return true ;
                case R.id.menu_people__search:
                    return true ;
                case R.id.menu_people__settings:
                    presenter.clickSettings() ;
                    return true ;
                case R.id.menu_people__logout:
                    sessionManager.logoutUser() ;
                    return true ;
            }
            return false ;
        }
    }) ;
} // end of setUpToolbar method


private void setUpRecyclerView() {
    if (DEBUG) Log.i(TAG, "Reached setUpRecyclerView") ;

    recyclerView.setHasFixedSize(true) ;

    final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()) ;
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL) ;
    recyclerView.setLayoutManager(layoutManager) ;
    recyclerView.setEmptyView(emptyView) ;

    adapter = new PeopleAdapter(realm.where(Person.class).equalTo("userId", userid).findAll()) ;
    adapter.setOnPersonClickListener(new PersonView.OnPersonClickListener() {
        @Override
        public void onPersonClick(Person person) {
            presenter.clickPerson(person) ;
        }

        @Override
        public void onPersonActionClick(final Person person) {
            presenter.clickPersonAction(person) ;

            // ToDo: this should be in the presenter, but I don't have realm in there yet
            final Person newFave = realm.where(Person.class)
                    .equalTo("id", person.getId())
                    .findFirst() ;
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    newFave.setIsFavorite(!person.getIsFavorite()) ;
                }
            }) ;
        }
    }) ;

    recyclerView.setAdapter(adapter) ;

} // end of setUpRecyclerView method

以下是PeopleAdapter的大部分内容:

class PeopleAdapter
    extends RealmRecyclerViewAdapter<Person, PeopleAdapter.PersonViewHolder> {

private final String TAG = "PeopleAdapter" ;
private final boolean DEBUG = true ;

private OrderedRealmCollection<Person> peopleList ;
private PersonView.OnPersonClickListener onPersonClickListener ;

static class PersonViewHolder extends RecyclerView.ViewHolder{

    PersonView personView ;

    PersonViewHolder(PersonView personView) {
        super(personView) ;
        this.personView = personView ;
    }
} // end of PersonViewHolder class


// Constructor
PeopleAdapter(OrderedRealmCollection<Person> data) {
    super(data, true, true) ; // data, autoUpdate, updateOnModification
    setHasStableIds(true) ;
    this.peopleList = data ;
}

void setOnPersonClickListener(PersonView.OnPersonClickListener onPersonClickListener) {
    this.onPersonClickListener = onPersonClickListener ;
}


@Override
public PeopleAdapter.PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    PersonView view = (PersonView) LayoutInflater.from(parent.getContext())
                                .inflate(R.layout.item_view_person, parent, false) ;
    return new PersonViewHolder(view) ;
}


@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
    holder.personView.setUser(peopleList.get(position)) ;
    holder.personView.setOnPersonClickListener(onPersonClickListener) ;
}


@Override
public int getItemCount() {
    // If the peopleList is null, return 0, otherwise return the number of items in the list
    return peopleList == null ? 0 : peopleList.size() ;
}

我到处都有日志消息,但我把它们中的大部分都用掉了,以便更容易阅读。基本上,无论我是这样尝试,还是尝试使用适配器实现可过滤,它都可以直接使用updateData方法而不会崩溃,抛出异常或指示任何问题,但它什么都不做。 SearchView出现,文本被输入并成为新查询的一部分,查询被移交给updateData,并且......没有。

Realm插件4.2.0,AndroidStudio 2.3.3,目标sdk 25,运行Android 7.0的三星Galaxy S2

我希望那里的人可以提供帮助,因为我尝试了像我这样的问题的答案,我感到很困惑。 谢谢

1 个答案:

答案 0 :(得分:1)

您的RealmRecyclerViewAdapter中没有peopleList变量,请使用getData()

编辑:这是你的适配器应该是这样的:

class PeopleAdapter
        extends RealmRecyclerViewAdapter<Person, PeopleAdapter.PersonViewHolder> {
    private final String TAG = "PeopleAdapter" ;
    private final boolean DEBUG = true ;

    private PersonView.OnPersonClickListener onPersonClickListener ;

    static class PersonViewHolder extends RecyclerView.ViewHolder{
        PersonView personView ;
        PersonViewHolder(PersonView personView) {
            super(personView) ;
            this.personView = personView ;
        }
    } // end of PersonViewHolder class


    // Constructor
    PeopleAdapter(OrderedRealmCollection<Person> data) {
        super(data, true, true) ; // data, autoUpdate, updateOnModification
    }

    void setOnPersonClickListener(PersonView.OnPersonClickListener onPersonClickListener) {
        this.onPersonClickListener = onPersonClickListener ;
    }


    @Override
    public PeopleAdapter.PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        PersonView view = (PersonView) LayoutInflater.from(parent.getContext())
                                    .inflate(R.layout.item_view_person, parent, false) ;
        return new PersonViewHolder(view) ;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder holder, int position) {
        holder.personView.setOnPersonClickListener(onPersonClickListener) ;
        holder.personView.setUser(getData().get(position)) ;
    }
}