getAdapter()。notifyDataSetChanged()不会更改recyclerview的数据库

时间:2019-04-08 04:33:16

标签: android firebase android-recyclerview recycler-adapter

我有2个按钮。在按钮上单击我决定要为recyclerview选择哪个数据库。例如,单击按钮B1时,我将数据库DB1设置为recyclerview。我正在使用firebaserecycleradapter。 问题是,用于recyclerview的getAdapter()。notifyDataSetChanged()在FirebaseRecyclerAdapter定义中似乎不起作用,但在populateviewholder中起作用。但是,由于我在FirebaseRecyclerAdapter定义上设置了查询,最后,单击按钮后,要选择的数据库不会更改。

以下是一些代码:

//global declaration
  FirebaseRecyclerAdapter<HomeBlog, BlogViewHolder> firebaseRecyclerAdapter;

//FirebaseRecyclerAdapter part:


             firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<HomeBlog, BlogViewHolder>(

                    HomeBlog.class,
                    R.layout.profile_row,
                    BlogViewHolder.class,
                    query

//the query is the Firebase database that I want at a time.
//since getAdapter().notifyDataSetChanged() not reloading from here. query remains same.

            )
            {
                @Override
                protected void populateViewHolder(BlogViewHolder viewHolder, HomeBlog model, int position) {


                    final String post_key = getRef(position).getKey();

                    final String postPosition = String.valueOf(position);

//getAdapter().notifyDataSetChanged() seems to load in populateviewholder though.
//this I checked using Toast.



                    viewHolder.setImage(post_key);

                    viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            //other code


                        }
                    });

                }
            };

            mBlogList.setAdapter(firebaseRecyclerAdapter);




//one button click example:
 grid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //changing the query

                query = mDatabase.orderByChild("UserId").equalTo(userId);
                mBlogList.setAdapter(firebaseRecyclerAdapter);
                mBlogList.getAdapter().notifyDataSetChanged();

            }
        });

我想确保在我调用getAdapter()。notifyDataSetChanged()时也加载了firebaserecycleradapter定义部分。因为它保存查询。 有某种方法可以做到吗?还是我做一个完全错误的过程?

1 个答案:

答案 0 :(得分:0)

调用setAdapter()之后,可以在该适配器上使用notifyDataSetChanged()。示例:

mBlogList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.notifyDataSetChanged();

然后您的数据将被更新。